The King’s Ups and Downs
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並按題目方式加入前邊n-1個數之間時,你可以有n個位置插入
1 2 3 4 5 6 7 加入n=8
0 1 2 3 4 5 6 7 可插8八個位置
當我們插入n時必須要滿足它的前邊高->低 - n - 低->高 因為加入的n是最大的。
如果我們我們取第i個位置插入那麼前i位置以前共有i個數,那麼這i個數將有C[n-1][i],即從前n-1個數中取i個數的可能方式數(排列組合)。
記錄前邊i個數中為高低排列的為dp[i][0],低高排列的為[i][1],由對稱性可知兩個是相等的且等於sum[i]/2.
那麼可以得到公式
AC程式碼:
#include <iostream> #include <cmath> using namespace std; typedef long long ll; ll c[100][100]; ll dp[25][2]; ll sum[100]; void solve(){ ll t=0; dp[0][0]=1; dp[0][1]=1; dp[1][0]=1; dp[1][1]=1; for(int i=2;i<=22;i++){ t=0; for(int j=0;j<=i;j++){ t+=dp[j][0]*dp[i-j-1][1]*c[i-1][j]; } sum[i]=t; dp[i][0]=dp[i][1]=t>>1; } return ; } int main(){ ll n,a,b; for(int i=0;i<=22;i++){ c[i][0]=c[i][i]=1; } c[1][1]=1; for(int i=2;i<=22;i++){ for(int j=1;j<i;j++){ c[i][j]=c[i-1][j-1]+c[i-1][j]; } } sum[0]=0; sum[1]=1; solve(); cin>>n; while(n--){ cin>>a>>b; cout<<a<<" "<<sum[b]<<endl; } }