hdu 4489 The King’s Ups and Downs(思维&DP)

来源:互联网 发布:elise兔兔的淘宝店 编辑:程序博客网 时间:2024/05/13 06:56

The King’s Ups and Downs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 151    Accepted Submission(s): 92


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
41 12 33 44 20
 

Sample Output
1 12 43 104 740742376475050
 

Source
Greater New York 2012
 

Recommend
liuyiding
 题意:
给你n个身高高低不同的士兵。问你把他们按照波浪状排列(高低高或低高低)有多少方法数。
思路:
开始脑残。觉得数据量不大才求20项想到了打表。于是用暴搜求答案。结果算到第15项就出不来答案了。
只能说自己太天真了。指数级增长肯定不能暴搜呀。不然dp就没用武之地了。
下来听了下别人的思路说是用dp。自己当时也想过用dp可惜没找到转移方程。后来想清楚后还是很简单的。
我们考虑。前面已经有i-1个人现在要放第i个人的情况。由于i是最后放的所以他一定是最高的。无论他在哪个位置一定都是波峰。
所以在他前面的部分结尾一定为高低。在他后面的部分开始一定为低高。这样才能形成高低低高。如果知道前面结尾高低的方法数n和后面开始低高的方法数m。那么在该位置的方法数就为n*m。而前面的个数和后面的个数是不确定的所以要枚举前面的个数j。而后面的个数就为i-j-1。由于状态转移要用到结尾高低的方法数和后面开始低高的方法数
所以我们用dp[i][0]表示有i个人且i个人结尾为高低的方法数。dp[i][1]表示有i个人且开始为低高的方法数。
那么有i个人排列的方法数就为ans[i]+=dp[j][0]*dp[i-j-1][1]*c[i-1][j]。c[i-1][j]为方法数。因为不确定前面j个人的标号。所以要在i-1个人中选j个人出来。开始一直纠结标号问题把自己都搞晕了。其实用第i个人把人分到两边。只这样只跟人数有关系系了。
但怎么更新状态转移需要用到的dp[i][1]和dp[i][0]呢?
把i个士兵排好后无非两种情况。开始为低高。开始为高低。那么排列的逆序也满足条件。也就是说结尾为高低的方法数开始为低高的方法数相同。而对于人数一定的情况。开始为低高的人数和开始为高低的人数相等。
证明:我们用0表示波谷1表示波峰。
当n为偶数时.假设波峰开始的序列为1010.那么把它倒置一下就变成了0101了。也就是说每一个1打头的对应着一个0打头的。
当n为奇数时。假设波峰开始的序列为10101.假设第一个数大于最后一个数。那我们把序列最后一个放到第一个位置由于第一个数大于最后一个数所以序列就变成01010.如果第一个数小于最后一个数把第一个数放到最后就行了。所以
每一个1打头的对应着一个0打头的。
所以dp[i][0]=dp[i][1]=ans[i]/2。这样就好递推了。。。
详细见代码:
#include <iostream>#include<stdio.h>using namespace std;__int64 dp[25][2],ans[25];__int64 c[20][25],t;int main(){    int i,j,cas,cs,n;    c[1][1]=1;    c[1][0]=1;    for(i=2;i<=20;i++)//算组合数    {        c[i][0]=c[i][i]=1;        for(j=1;j<i;j++)          c[i][j]=c[i-1][j]+c[i-1][j-1];    }    dp[1][0]=dp[1][1]=dp[0][0]=dp[0][1]=1;//初始为1    for(i=2;i<=20;i++)    {        t=0;//t记录总方法数        for(j=0;j<i;j++)        {            t+=dp[j][0]*dp[i-j-1][1]*c[i-1][j];        }        dp[i][0]=dp[i][1]=t/2;    }    scanf("%d",&cas);    while(cas--)    {        scanf("%d%d",&cs,&n);        if(n==1)        {            printf("%d 1\n",cs);            continue;        }        printf("%d %I64d\n",cs,dp[n][0]<<1);    }    return 0;}/*#include <iostream>#include<stdio.h>using namespace std;__int64 ans[20]={1,2,4,10,32,122,544,2770,15872,101042,707584,5405530,44736512,398721962LL,3807514624LL,38783024290LL,419730685952LL,4809759350882LL,58177770225664LL,740742376475050LL};int main(){    int t,c,n,cas=1;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&c,&n);        printf("%d %I64d\n",cas++,ans[n-1]);    }}*/

感想:
自己的dp功底还不够呀,很多题目还不能一下子把状态抽象出来。接下来还得多加练习。。。