首页 技术 正文
技术 2022年11月18日
0 收藏 898 点赞 2,162 浏览 2661 个字

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4489

The King’s Ups and Downs

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
#### 问题描述
> The king has guards of all different heights. Rather than line them up in increasing or decreasing height order, he wants to line them up so each guard is either shorter than the guards next to him or taller than the guards next to him (so the heights go up and down along the line). For example, seven guards of heights 160, 162, 164, 166, 168, 170 and 172 cm. could be arranged as:
> or perhaps:
> The king wants to know how many guards he needs so he can have a different up and down order at each changing of the guard for rest of his reign. To be able to do this, he needs to know for a given number of guards, n, how many different up and down orders there are:
>
> For example, if there are four guards: 1, 2, 3,4 can be arrange as:
>
> 1324, 2143, 3142, 2314, 3412, 4231, 4132, 2413, 3241, 1423
>
> For this problem, you will write a program that takes as input a positive integer n, the number of guards and returns the number of up and down orders for n guards of differing heights.
#### 输入
> The first line of input contains a single integer P, (1 For each data set there is one line of output. It contains the data set number (D) followed by a single space, followed by the number of up and down orders for the n guards.

样例输入

4

1 1

2 3

3 4

4 20

样例输出

1 1

2 4

3 10

4 740742376475050

题意

给你n个数(1到n),问排出高低高,,,或低高低,,,的所有情况数。

题解

考虑最后一个数n摆放的位置,枚举在它左边放x个数,右边放n-1-x个数,然后转移。

需要用到一个结论:对于任意的n>=2,高低高和低高低的情况数都是相等的!

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printftypedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);//start----------------------------------------------------------------------const int maxn=22;LL dp[maxn],C[maxn][maxn];void get_C(){
C[0][0]=1;
for(int i=1;i<maxn;i++){
C[i][0]=1;
for(int j=1;j<=i;j++){
C[i][j]=C[i-1][j-1]+C[i-1][j];
}
}
}void pre(){
get_C();
dp[0]=dp[1]=1;
dp[2]=2;
for(int i=3;i<=20;i++){
for(int x=0;x<i;x++){
LL lef=x<=1?dp[x]:dp[x]/2;
int y=i-x-1;
LL rig=y<=1?dp[y]:dp[y]/2;
dp[i]+=C[i-1][x]*lef*rig;
}
}
}int main() {
pre();
int tc;
scanf("%d",&tc);
while(tc--){
int kase,n;
scf("%d%d",&kase,&n);
prf("%d %lld\n",kase,dp[n]);
}
return 0;
}//end-----------------------------------------------------------------------
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918