ZOJ3802 Easy 2048 Again
来源:互联网 发布:淘宝衣服怎么拍照技巧 编辑:程序博客网 时间:2024/05/21 14:10
Easy 2048 Again
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
3
4
2 8 4 8
5
2 2 4 8 16
8
8 4 4 2 8 4 2 2
Sample Output
34
92
116
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).
题意:一维版的2048,给你n个数,从中任选几个(相对顺序不变),选一个得多少分,如果可以和边上的合并,再加上这两个分数,求最大的分数。
这题好像一个背包一样的,就是求这个背包里怎么放使分数最大,把背包看成一个栈,后放入的数在栈顶,所以我们发现放入一个数只和他的前面的一个数有关,这个状态就用状态压缩来表示,把分数有几个多少分看成一个状态(最大16X500),所以我们用滚动数组维护这个背包(小心MLE),枚举原来背包里的数(不存在就是-1),与目前的数比较,看是否相同,相同合并就是。。。
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN=8010;int dp[MAXN][2],num[510];int main(){ int T,n,i,j; scanf("%d",&T); while(T--) { memset(dp,-1,sizeof(dp)); scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&num[i]); dp[0][0]=dp[0][1]=0; int ans=-1; for(i=1;i<=n;i++) { for(j=0;j<8010;j++) { if(dp[j][!(i&1)]==-1) continue; dp[j][i&1]=max(dp[j][i&1],dp[j][!(i&1)]); int cur=num[i]; int t; if(j&(cur-1)) t=cur; else t=j+cur; int sum=0; int p=j; if(!(j&(cur-1))) while(p&cur) { p-=cur; cur*=2; sum+=cur; } if(dp[j][!(i&1)]+sum+num[i]>dp[t][i&1]) dp[t][i&1]=dp[j][!(i&1)]+sum+num[i]; ans=max(ans,dp[t][i&1]); } } printf("%d\n",ans); } return 0;}
0 0
- ZOJ3802 Easy 2048 Again
- zoj3802 Easy 2048 Again 状压dp
- zoj-3802-Easy 2048 Again
- zoj-3802-Easy 2048 Again
- Easy 2048 Again - ZOJ 3802 状压dp
- ZOJ 3802 Easy 2048 Again(状压dp)
- zoj 3802 Easy 2048 Again (动态规划)
- ZOJ 3802 Easy 2048 Again 状压DP
- ZOJ -- 3802 Easy 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 ( 状态压缩 )
- ZOJ 3802Easy 2048 Again(状压DP)
- 动态规划——Easy 2048 Again
- zoj 3802 Easy 2048 Again (14.8 浙大月赛 E)
- zoj 3802 Easy 2048 Again(状压DP)
- ZOJ 3802 Easy 2048 Again(压缩dp)
- 公司法定搞好宏观
- Hibernate的使用
- Objective-C中的Literals
- C语言数组
- 利用AsyncHttpClient实现图片的上传与下载
- ZOJ3802 Easy 2048 Again
- Eclipse 开发过程中利用 JavaRebel 提高效率
- poj 2330 Watchcow(欧拉回路求点序列)
- Android开发(十四) Android Manifest.xml配置访问权限设置
- 需求获取过程
- RailsCasts10 Refactoring User Name Part 1 重构User Name(一)
- web 开发,前台验证
- Oracle 特殊符号'&'的处理
- [Linux Input]even-codes.txt input事件类型