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

题意:对于一个序列,你能够按顺序选或不选一些数。然后放在一个栈里。假设两个一样的数在一起就会合并,你的得分是放的数加上合并成的数的总和,问最后得分最多是多少。

思路:状压dp,这道题本身就是二进制,也就方便了,注意要用滚动数组。不然会爆MLE。

这里用滚动数组记录你在选第n个数的时候之前的状压情况和得分。

AC代码例如以下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
map<ll,ll> match[2];
ll F[1000010][2];
int vis[1000010];
ll ans;
void solve(ll S,ll k,int pos,int pos2)
{ F[0][pos2]++;
F[F[0][pos2]][pos2]=S; //不选这个数,保留原状压情况
match[pos2][S]=max(match[pos2][S],match[pos][S]);
if((S%k)!=0) //选这个数。而且之前状压情况有比k小的数。比方之前的状压结果为8 4 2 这时你增加一个4,那么队列里面就会是 8 4 2 4,
//前三个数都不可能再去合并了。所以状压结果仅仅要保留k就能够了
{ match[pos2][k]=max(match[pos2][k],match[pos][S]+k);
F[0][pos2]++;
F[F[0][pos2]][pos2]=k;
ans=max(ans,match[pos2][k]);
return;
}
ll ret=k,S2=S;
while((S&k)!=0) //合并同样的数
{ S-=k;
k*=2;
ret+=k;
}
S+=k;
match[pos2][S]=max(match[pos2][S],match[pos][S2]+ret);
ans=max(ans,match[pos2][S]);
F[0][pos2]++;
F[F[0][pos2]][pos2]=S;
}
int main()
{ int t,n,i,j,k,len,a,b,pos=0;
scanf("%d",&t);
while(t--)
{ scanf("%d",&n);
match[0].clear();match[1].clear();
F[0][0]=1;F[1][0]=0;
match[0][0]=0;
ans=0;
for(i=1;i<=n;i++)
{ pos++;
scanf("%lld",&k);
if(i&1)
{ a=0;b=1;}
else
{ a=1;b=0;}
match[b].clear();
F[0][b]=0;
for(j=1;j<=F[0][a];j++) // F[0][a]记录之前有多少种情况
if(vis[F[j][a]]!=pos)
{ solve(F[j][a],k,a,b); //对之前状压结果为F[j][a]的情况考虑选或不选这个数的结果
vis[F[j][a]]=pos;
}
}
printf("%lld\n",ans);
}
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

Easy 2048 Again - ZOJ 3802 像缩进dp的更多相关文章

  1. ZOJ3802 Easy 2048 Again (状压DP)

    ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...

  2. HDU 3681 BFS&amp;像缩进DP&amp;二分法

    N*M矩阵.从F出发点.走完全部Y点.每个人格开支1电源点,去G点,电池充满,D无法访问.最小的开始问什么时候满负荷可以去完全部Y.Y和G总共高达15一 第一BFS所有的F.Y.G之间的最短距离. 然 ...

  3. ZOJ Problem Set - 3822Domination(DP)

    ZOJ Problem Set - 3822Domination(DP) problemCode=3822">题目链接 题目大意: 给你一个n * m的棋盘,每天都在棋盘上面放一颗棋子 ...

  4. ZOJ 3802 Easy 2048 Again 像缩进DP

    链接:problemId=5334">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5334 题意:一个长度为5 ...

  5. ZOJ 3802 Easy 2048 Again 状态DP

    zoj 上次的月赛题,相当牛的题目啊,根本想不到是状态压缩好吧 有个预先要知道的,即500个16相加那也是不会超过8192,即,合并最多合并到4096,只有2的12次方 所以用状态压缩表示前面有的序列 ...

  6. zoj3802:easy 2048 again(状压dp)

    zoj月赛的题目,非常不错的一个状压dp.. 题目大意是一个一维的2048游戏 只要有相邻的相同就会合并,合并之后会有奖励分数,总共n个,每个都可以取或者不取 问最终得到的最大值 数据范围n<= ...

  7. Easy 2048 Again(状压dp)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3802 题意: 从数列A中, 删除若干个数(可以0个), 是删除 ...

  8. zoj 3537 Cake 区间DP (好题)

    题意:切一个凸边行,如果不是凸包直接输出.然后输出最小代价的切割费用,把凸包都切割成三角形. 先判断是否是凸包,然后用三角形优化. dp[i][j]=min(dp[i][j],dp[i][k]+dp[ ...

  9. ZOJ 3623 Battle Ships DP

    B - Battle Ships Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Subm ...

随机推荐

  1. hnnu 11546 Sum of f(x) (求一个数的全部约数和)

    代码: #include<cstdio> #include<cstring> #define N 200000 using namespace std; long long f ...

  2. C / C++算法学习笔记(7)-双向冒泡

    原始地址:双向冒泡 通常的冒泡是单向的,而这里是双向的,也就是说还要进行反向的工作. 代码看起来复杂,仔细理一下就明白了,是一个来回震荡的方式. 写这段代码的作者认为这样可以在冒泡的基础上减少一些交换 ...

  3. 奋斗的孩子的TableView(三篇文章)

    http://blog.sina.com.cn/s/blog_a6fb6cc90101i8it.html http://blog.sina.com.cn/s/blog_a6fb6cc90101hhse ...

  4. Java时间比較

    Date类有两个方法 一个是after()比方date1.after(date2)推断date1是否在date2之后也就是说date1小于date2吧, 一个是before()比方date1.befo ...

  5. spark的action和transformations汇集

    汇总了Spark支持的Transformations 和Actions 用于备忘! 參考 http://spark.apache.org/docs/latest/programming-guide.h ...

  6. cocos2d-js-v3.0-rc0 下 pomelo-cocos2d-jsb native web 配置

    一.基本步骤 注意:pomelo-cocos2d-jsb 没实用 https://github.com/NetEase/pomelo-cocos2d-jsb,原因这个不是最新版,另外,componen ...

  7. hdu 2451 Simple Addition Expression(数位DP )成败在于细节

    亚洲区域赛的题,简单的数位DP题,注重细节. 任何细节都有可能导致wa,所以没有绝对的水题. 把握好细节,此题便A. #include<stdio.h> __int64 getans(__ ...

  8. Cygwin下vim按方向键出现ABCD;

    1:乱码解决Option->Text设置编码 2:vim按方向键出现A.B.C.D 解决:--$ cd /usr/share/vim/vim73 (ps:看你的版本号.假设没有这个文件可能是/u ...

  9. 关于在ios7之后改变状态栏颜色

    看到网上都说 在ios7之后要这样设置 首先,须要在Info.plist配置文件里,添加键:UIViewControllerBasedStatusBarAppearance,并设置为YES: 然后,在 ...

  10. SVN的switch命令

    语法就不说了,文档有的是,主要是两个常用的用法: . 切换资源库(svn sw --relocate) [plain] view plaincopy svn sw --relocate <fro ...