欢迎访问https://blog.csdn.net/lxt_Lucia~~
宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~
----------------------------------------我只是一条可爱哒分界线-------------------------------------------
一、问题:
Description
The King’s Ups and Downs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 389 Accepted Submission(s): 260
Problem Description
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.
Input
The first line of input contains a single integer P, (1 <= P <= 1000), which is the number of data sets that follow. Each data set consists of single line of input containing two integers. The first integer, D is the data set number. The second integer, n (1 <= n <= 20), is the number of guards of differing heights.
Output
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.
Sample Input
4
1 1
2 3
3 4
4 20
Sample Output
1 1
2 4
3 10
4 740742376475050
二、题意:
有 n 个人,身高分别为 1 ~ n ,将这n个人高矮不同的人排成高矮相间的一排(即 高矮高矮.. 或 矮高矮高..),求有多少种排法。
三、思路:
本篇介绍三种思路:
1)暴力。n 的范围为 (1 <= n <= 20),将这20个都算出来就可以了,要哪个输出哪个。
2)dp推:(参考https://blog.csdn.net/niuox/article/details/8866907 )
dp( n , k ) 表示:长度为 n ,最后一个数为k,最后两个数是递增的 排列的个数;
dp2( n , k )表示:长度为 n ,最后一个数为k,最后两个数是递减的 排列的个数;
那么:dp( n , k ) = dp2( n , n + 1 - k ) ;
很好理解吧,比如说132(低高低)等价于312(高低高),相对的位置加起来等于4.
那么我们针对dp[ n ][ k ]的最后一位进行如下考虑:
最后一位是k,因为dp[ n ][ k ]最后两个数字是递增的,所以第n-1位的最大值是k-1。那么我们很容易推导出DP方程:
又:
所以:dp( n , k ) = dp( n - 1 , n + 1 - k ) + dp( n , k - 1 );
3)组合dp:(参考https://blog.csdn.net/yu_ch_sh/article/details/48479495 )
对于第 n 个人,假设前面的 n - 1 个人已经放好了。第n个人有n个位置可放,对于任一位置j,显然第n个人的身高n大于前n-1个人的任何人的身高。所以第 n 个人左边的 j - 1个人的排列中,必须满足最后一个人一定是通过身高下降得到的,右边的n-j个人中,最开始的那个人一定通过升高得到后面一个人的,当然前面j-1个人可选C(n - 1 , j - 1 )。
所以设状态dp[ i ][ 0 ]表示有 i 个人,并且第一个人通过上升得到第二个人的总的排列种数,i个人身高肯定不一样,故只考虑个数。dp[ i ][ 1 ]表示有 i 个人,并且最后一个人是通过下降得到的。
很显然在人数相同的情况下,由对称性得 dp[ i ][ 0 ] = dp[ i ][ 1 ]=sum[ i ] / 2 sum[i]为i个人总的满足要求的排列数。
对于第 n 个人放到位置 j ,有C ( n - 1 , j - 1 ) * dp[ j - 1 ][ 0 ] * dp[ n - j ][ 1 ]种情况。
四、代码:
1)思路一:
#include <cstdio>
#define read(x) scanf("%d",&x)
typedef long long LL;
LL a[21]={0,1,2,4,10,32,122,544,2770,15872,101042,707584,5405530,44736512,398721962,3807514624,38783024290,419730685952,4809759350882,58177770225664,740742376475050};
int main()
{
int T, k, n;
read(T);
while(T--)
{
read(k),read(n);
printf("%d %lld\n", k, a[n]);
}
return 0;
}
2)思路二:
#include <cstdio>
#include <cstring>
#define read(x) scanf("%d",&x)
#define mem(a,b) memset(a,b,sizeof(a))
#define fori(a,b) for(int i=a;i<=b;i++)
#define forj(a,b) for(int j=a;j<=b;j++)
using namespace std;
typedef long long LL;
int main()
{
int T, k, n;
LL a[21], dp[21][21];
mem(a,0), mem(dp,0);
a[1] = dp[1][1] = 1;
fori(2,20)
{
forj(2,i)
{
dp[i][j] = dp[i-1][i-j+1] + dp[i][j-1];
a[i] += dp[i][j] * 2;
}
}
read(T);
while(T--)
{
read(k),read(n);
printf("%d %lld\n", k, a[n]);
}
return 0;
}
3)思路三:
#include <cstdio>
#include <cstring>
#define read(x) scanf("%d",&x)
#define fori(a,b) for(int i=a;i<=b;i++)
#define forj(a,b) for(int j=a;j<=b;j++)
using namespace std;
typedef long long LL;
LL dp[25][2], sum[25];
LL c(int a, int b)
{
LL ans = 1;
if(a > b/2)
b = a - b;
fori(1, b)
{
ans *= (a-i+1);
ans /= i;
}
return ans;
}
int main()
{
int T, m, n;
dp[0][0] = dp[0][1] = 1;
dp[1][0] = dp[1][1] = 1;
sum[1] = 1;
fori(2, 20)
{
sum[i] = 0;
forj(1, i)
sum[i] += (dp[j-1][0] * dp[i-j][1]) * c(i-1,j-1);
dp[i][0] = dp[i][1] = sum[i]/2;
}
read(T);
while(T--)
{
read(n), read(m);
printf("%d %lld\n", n, sum[m]);
}
return 0;
}
------------------------------------------我也是有底线的----------------------------------------------------