[POJ3802]状态压缩DP 2048合并 flappy2048

来源:互联网 发布:javascrip 和js 编辑:程序博客网 时间:2024/05/18 03:56

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 integerL, which is the length of the road.(0 < L ≤ 500) The second line containsL 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


之前写的一份状态转移还是有点问题,最后还是高仿了一份,坑啊,还是太渣~

#define DeBUG#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <string>#include <set>#include <sstream>#include <map>#include <list>#include <bitset>using namespace std ;#define zero {0}#define INF 0x3f3f3f3f#define EPS 1e-6#define TRUE true#define FALSE falsetypedef long long LL;const double PI = acos(-1.0);//#pragma comment(linker, "/STACK:102400000,102400000")inline int sgn(double x){    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);}#define N 505int num[N];int a[N];int pow2[30];int zhishu[30];void init(){    memset(zhishu, 0, sizeof(zhishu));    for (int i = 2, j = 1; i <= 16; i *= 2, j++)    {        zhishu[i] = j;    }    memset(pow2, 0, sizeof(pow2));    pow2[0] = 1;    for (int i = 1; i <= 15; i++)    {        pow2[i] = pow2[i - 1] * 2;    }}int dp[2][5000];int main(){#ifdef DeBUGs    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);#endif    init();    int T;    scanf("%d", &T);    while (T--)    {        int n;        scanf("%d", &n);        for (int i = 1; i <= n; i++)        {            scanf("%d", &num[i]);            a[i] = zhishu[num[i]];        }        memset(dp, -1, sizeof(dp));        int pre = 1, now = 0;        dp[1][1 << a[1]] = num[0];        dp[1][0] = 0;        for (int i = 1; i <= n; i++)        {            for (int j = 0; j <= 4096; j++)            {                if (dp[pre][j] != -1)                {                    // printf("%d=%d ", j,dp[i-1][j]);                    dp[now][j] = max(dp[now][j], dp[pre][j]);                    int k = a[i] - 1;                    int get = pow2[a[i]];                    int next = j;                    if ((j & (pow2[k] - 1)) == 0)                    {                        while (j & pow2[k])                        {                            get += pow2[k + 2];                            k++;                        }                        int q = pow2[k] - 1;                        next = (j & (~q));                        next |= pow2[k];                    }                    else next = pow2[k];                    dp[now][next] = max(dp[now][next], dp[pre][j] + get);                }            }            // printf("\n");            swap(pre, now);        }        int ans = -INF;        for (int i = 0; i <= 4096; i++)        {            // if (ans < dp[n - 1][i])            //     printf("i=%d dp=%d\n", i, dp[n - 1][i]);            ans = max(ans, dp[pre][i]);        }        printf("%d\n", ans);    }    return 0;}


0 0
原创粉丝点击