[zoj 3802]Easy 2048 Again 状压DP

来源:互联网 发布:李白 知乎 编辑:程序博客网 时间:2024/06/01 10:48

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

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

题目大意 
给定L个数,从中任意去定一些数。使得分数最大。
分数计算方法为,所用被装进背包的数字和为基础分,并且按照游戏2048的规则,每次压入队尾,一但顶队尾个值相同,则队尾变为其两倍,并加上对应的分数。
问最大值是多少。
解题思路
sum值最大不会超过8192=2^13
则背包可行状态一定是一个单调减的栈
如10表示背包的尾部存在 8 2 两个数字
滚动数组状压可不可以压进新的值(比队尾的数字更小或相等)更新状态,如果不能压入则将单调减栈的最大值定为a[i]即可。
滚动数组存储状态。
每次计算combo的分数。

  #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 */


0 0
原创粉丝点击