XTU OJ 1168 Alice and Bob (二维dp)
2014-08-17 14:59
218 查看
Alice and Bob | ||
Accepted : 98 | Submit : 324 | |
Time Limit : 1000 MS | Memory Limit : 65536 KB |
Problem DescriptionAlice and Bob always love to play games, so does this time.It is their favorite stone-taken game. However, this time they does not compete but co-operate to finish this task. Suppose there is a stack of n stones. Each turn, Alice can only take away stones in number pow of 2, say 1, 2, 4, 8, 16, ... Bob can only take away stones in number pow of 3, say 1, 3, 9, 27, 81, ... They takes stones alternately, and lady first. Notice in each turn, Alice/Bob have to take away at least one stone, unless the stack is empty. Now, the question is, what is the least number of operation for taking away all the stones. InputMultiple test cases. First line, there is an integer T ( 1 ≤ T ≤ 20 ), indicating the number of test cases.For each test case, there is a number n ( 1 ≤ n ≤ 10000 ), occupying a line, indicating the total number of the stones. OuputFor each test case, output a line. It is an integer number k, indicating the least number of operation in need to finish the task.Sample Input5 1 2 3 4 5 Sample Output1 1 2 1 2 SourceXTU OnlineJudge去年看到这道题目的时候对动态规划没有一点思路,不知道该怎么往那方面想,对动态规划有种畏惧的感觉。重新来做这道题目,往那方面想还是得不出递推方程式啊,动态规划还是得多练,还需要有一定的灵感;看了一下别人的思路,终于看懂了,http://blog.csdn.net/libin56842/article/details/26287101 用自己的风格写了一遍; 这里用的是二维dp,用dp[i][0]:alice 取i颗石子所用的最小步数(alice是先手); dp[i][1]:bob 取i颗石子所用的最小步数; alice先取石子,如果alice一次没有取完,bob就接着取,我们要得到的是取完所用的最小步数,所以我们可以得到状态关系式; min(dp[i][0],dp[i-j][1]+1); j就是Alice取走的颗数,bob接着取次数就要加1; 下面是代码: #include <cstdio> #include <cstring> #define min(a,b) a<b?a:b const int maxn=10005; const int Max=0x3f3f3f3f; int dp[maxn][2]; int main() { int t,n,i,j; dp[0][0] = dp[0][1] = 0; dp[1][0]=dp[1][1]=dp[2][0]=1;//一次可以取完 dp[2][1]=2;//bob要取两次 for(i=3;i<maxn;i++)//这里我们就要从3开始了 { dp[i][0]=dp[i][1]=Max;//初始值赋为最大值 for(j=1;j<=i;j*=2)//alice每次取的是2的j次 dp[i][0]=min(dp[i][0],dp[i-j][1]+1);//比较直接的最小值 for(j=1;j<=i;j*=3)//bob取的 dp[i][1]=min(dp[i][1],dp[i-j][0]+1); } scanf("%d",&t); while(t--) { scanf("%d",&n); printf("%d\n",dp [0]);//这里按的是alice为先手算的 } return 0; } |
相关文章推荐
- XTU1168:Alice and Bob(二维DP)
- uva1484 Alice and Bob's Trip (树形dp)
- 【XSY2190】Alice and Bob VI 树形DP 树剖
- UVA 1484 - Alice and Bob's Trip(树形DP)
- hdu - 3660 - Alice and Bob's Trip(树形dp)
- hdu4268 Alice and Bob(二维贪心的固定思想 STL的multiset的二分查找函数实现)
- UVALive 5088 Alice and Bob's Trip(树形DP)
- UVALive 5088 Alice and Bob's Trip(树形DP)
- 树形dp-hdu-3660-Alice and Bob's Trip
- UVALive - 5088 Alice and Bob's Trip 树形DP
- hdoj 3660 Alice and Bob's Trip(树dp)
- HDU 4111 Alice and Bob 【DP解决博弈】
- XTU OJ 1209 Alice and Bob 2014(嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛)
- UVA1484 Alice and Bob's Trip(树形dp)
- uva 1484 - Alice and Bob's Trip(树形dp)
- uvalive 5760 Alice and Bob (组合游戏,dp)
- UVA 1484 Alice and Bob's Trip (树形dp)
- XTOJ 1168 Alice and Bob (记忆化搜索)
- [HDU] 3660 Alice and Bob's Trip -- 树形DP?
- uva 1484 Alice and Bob's Trip (树形dp)