[zoj 3802]Easy 2048 Again 状压DP
来源:互联网 发布:李白 知乎 编辑:程序博客网 时间:2024/06/01 10:48
Dark_sun knows that on a single-track road (which means once he passed this area, he cannot come back again), there are some underground treasures on each area of the road which has the value of 2, 4, 8 or 16. Dark_sun can decide whether to dig them or not in order to get the highest score. The calculation rule of the total score is similar to the game Flappy 2048.
Dark_sun's bag is like a queue. When he gets a treasure, the treasure will be pushed back into the end of the bag. And the score will add the value of the treasure. For example, when there are treasures on the road in the order of {2, 8, 4, 8} and if Dark_sun decides to dig all of them, he will get the final score of 2+8+4+8=22. And his bag will finally become the sequence of {2, 8, 4, 8}.
If the adjacent treasures in the Dark_sun's bag have the same value, they will mix into a bigger treasure which has the value of their sum (the double value of the previous one). And Dark_sun will get a combo score of the value of bigger treasure. For example in the previous case, if Dark_sun decides to dig only the {2, 8, 8} treasure in sequence. He will get the basic score of 18(2+8+8). And when the last treasure (value 8) is pushed back into his bag, his bag will turn {2, 8, 8} into {2, 16} and he will get a bonus score of 16. And his final score will become 18+16=34 (which is the best strategy in this case.)
Notice that the treasures mix to the bigger one automatically when there are the same adjacent treasures. For example, when there are treasures of {2, 2, 4, 8, 16} on the road, and if Dark_sun decides to dig all of them, he will get the basic score of 32(2+2+4+8+16) and a bonus score of 60(4+8+16+32). At last he will get the total score of 92 and the bag becomes {32}.
Now, Dark_sun wants to get the highest score (no matter what's the treasure in his bag), can you tell him the what's the highest score?
Input
The first line is an integer n, which is the case number. In each case, the first line is an integer L, which is the length of the road.(0 < L ≤ 500) The second line contains L integers which can only be 2, 4, 8 or 16. This means the value of treasure on each area of the road.
Output
For each case, you should output an integer. The answer is the maximum of the total score which Dark_sun may get.
Sample Input
342 8 4 852 2 4 8 1688 4 4 2 8 4 2 2
Sample Output
3492116
Hint
In the third sample case, Dark_sun will choose {8,4,4,8,4,2,2}. Firstly, the first three treasures will be combined to 16 and then the {16,8,4,2,2} will become 32. And he will get the basic score 32(8+4+4+8+4+2+2) and the bonus score 84(8+16+4+8+16+32).
Author: Ni, Xinyi
Source: ZOJ Monthly, August 2014
#include <cstdio> #include <iostream> #include <algorithm> #include <ctime> #include <cctype> #include <cmath> #include <string> #include <cstring> #include <stack> #include <queue> #include <list> #include <vector> #include <map> #include <set> #define sqr(x) ((x)*(x)) #define LL long long #define INF 0x3f3f3f3f #define PI acos(-1.0) using namespace std; int dp[2][10000]; int check3[10000]; int check(int x,int y) { int tmp=0; while (x&y) { tmp+=y*2; x-=y; y*=2; } return tmp; } int main() { memset(check3,0,sizeof check3); for (int j=1;j<=9000;j=j*2) for (int i=1;i*j<=9000;i++) { check3[i*j]=j; } int T,n,m; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(dp,0,sizeof dp); int o; for (int i=1;i<=n;i++) { int en=i&1; int st=en^1; for (int j=0;j<=8192;j++) { dp[en][j]=max(dp[st][j],dp[en][j]); } scanf("%d",&o); for (int j=0;j<=8192;j++) if (check3[j]<o){ dp[en][o]=max(dp[st][j]+o,dp[en][o]); }for (int j=0;j<=8192;j++) if (dp[st][j]) { if (check3[j]>=o){ dp[en][o+j]=max(dp[st][j]+o+check(j,o),dp[en][o+j]); } } int ans=0;for (int j=0;j<=8192;j++) { ans=max(dp[en][j],ans); } if (i==n) printf("%d\n",ans); } } return 0; } /*342 8 4 852 2 4 8 1688 4 4 2 8 4 2 2 */
- Easy 2048 Again - ZOJ 3802 状压dp
- ZOJ 3802 Easy 2048 Again(状压dp)
- ZOJ 3802 Easy 2048 Again 状压DP
- ZOJ -- 3802 Easy 2048 Again(状压dp)
- [zoj 3802]Easy 2048 Again 状压DP
- ZOJ 3802Easy 2048 Again(状压DP)
- 【DP】 ZOJ 3802 Easy 2048 Again
- ZOJ 3802 Easy 2048 Again(简单DP)
- zoj 3802 Easy 2048 Again(状压DP)
- ZOJ 3802 Easy 2048 Again(压缩dp)
- ZOJ 3802 Easy 2048 Again 状态压缩dp
- zoj-3802-Easy 2048 Again
- zoj-3802-Easy 2048 Again
- ZOJ 3802 Easy 2048 Again(状压DP+位运算)【一维状压类模板--2048】
- zoj 3802 Easy 2048 Again (动态规划)
- ZOJ 3802 Easy 2048 Again ( 状态压缩 )
- ZOJ-3802:Easy 2048 Again(2048游戏 状态压缩dp)
- ZOJ-3802-Easy 2048 Again【状态压缩dp】【位运算】【好题】
- 生产者消费者问题(多进程+共享内存+信号量)
- 浅谈三层
- 华为机试---删除多行的重复数字
- JSP页面的动态包含和静态包含示例及介绍
- Add Two Numbers Leetcode
- [zoj 3802]Easy 2048 Again 状压DP
- 01背包问题
- 字符串匹配——KMP算法
- POJ 3084 Panic Room(最小割)
- poj 3764字典树应用
- 响应式设计和HTML的<picture>标签
- zoj 3672 思维题
- java compiler level does not match the version of the installed java project
- windows程序设计笔记(1)