动态规划——Easy 2048 Again

来源:互联网 发布:淘宝旺旺名是什么 编辑:程序博客网 时间:2024/06/03 19:17

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).


题意:从给的数中选一些数进行2048游戏。。。求分数最大可以是多少

思路:状态压缩和滚动数组。。。看了各位大神的题解,花了一个上午和下午。。。orz

智商堪忧。

dp数组用滚动数组优化,第二维只用开到8000就行,因为最大也就是500个16进行游戏,况且500个16也不能全都合并所以最大的合并数小于8000.

具体见代码

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int a[550];int dp[2][8000];int main(){    int t;    scanf("%d", &t);    while(t--)    {        int n;        scanf("%d", &n);        for(int i=1; i<=n; i++)            scanf("%d", &a[i]);        memset(dp, -1, sizeof(dp));        dp[0][0]=0;        int ans=0;        int pos=1;                     //表示滚动数组的下标        for(int i=1; i<=n; i++)        {            for(int j=0; j<8000; j++)            {                if(dp[pos^1][j]==-1)   //假如前一组没出现过j这个状态就跳过                    continue;                dp[pos][j]=max(dp[pos][j], dp[pos^1][j]);                ans=max(ans, dp[pos][j]);                int sum=a[i];            //当前状态的分数                int t=j;                if((t&(a[i]-1))==0)     //因为a[i]是2的某一次幂,所以减一后与t做&                {                             //来保证j状态大于a[i]                    int k=a[i];                    while(t&k)                                 sum+=(k<<=1);     //合并                    t&=~(k-1);               //将t小于k的位置为0                    t|=k;                      //如果t的等于k的位是0就加上k                }                else                    t=a[i];                dp[pos][t]=max(dp[pos][t], dp[pos^1][j]+sum);                   ans=max(ans, dp[pos][t]);            }            pos^=1;       //下标滚动到下一组        }        printf("%d\n", ans);    }    return 0;}