题目

Easy 2048 Again —–zoj 3802

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


题意

给出一个序列,里面的值有2、4、8、16组成,现在要按顺序有选择地将数放入包中。
可以将包想象成一个栈。
如果包中两个相邻数x相同,那么会自动合并成2*x,并且得到奖励分数2*x。(可以连续合并)
最后得分=所放入包中的数值之和+奖励分数。
问最后得分最大为多少?
         
(序列长度不超过500)


思路

我将16 8 4 2简化为 8 4 2 1,这样只需将最后的答案*2,即可还原。
 
观察数字合并的过程,发现如果包中存放32 16 8 4 2显然可以合并
特征是包中数递减,并且后面存的数是前面的1/2。
 
那么有一种想法是这样表示状态:记录最后一个放入的数2,然后记录可连续合并的长度5,dp[2][5]这样解题。
 
可惜这样做得到了wa,
后来分析32 8 4 2 2这样的情形仍然有机会将32合并,因为包中的数可变为32 16,所以32仍然有机会合并。特征是数递减,并且没有重复,否则已经合并
 
但是前面那种状态表示的方法已经不能解决这个问题
 
分析会发现包中最大的数为 500*8=4000,由于 212=4096>4000 2^{12}=4096>4000,故数最大只可能为 211 2^{11}。
这样会发现需要保存的之前的数长度非常有限,而且由于都是2的幂次,状态压缩可解。
 
(之前的做法问题在于没法保存32 8 4 2中的32这样的数,因为状态表示是dp[8][3],现在正好解决了这个问题)
 
一旦状态找到dp题就做了一大半,剩下就好做了。
 
这个题目需要用到滚动数组,因为一个阶段到下一个阶段的多个状态进行转移时,没有可以依照的顺序,必须开数组存放结果。


代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mem(a,x)  memset(a,x,sizeof a)
#define ysk(x)  (1<<(x))
const int maxn=500  ;
const int maxS= ysk(12)-1  ;//最大状态(状压),估计值,实际此题达不到
int n,dp[2][maxS+5];//2代表滚动数组int lowbit(int x)  {  return x&(-x);} //提取x最后一个1,比如110110(二进制),那么返回的结果是10(二进制),即2(十进制)。int cal(int s,int k)//计算一段连续可消的数的奖励分数,即8 4 2如果遇到一个2,那么奖励分数是4+8+16
{int ans=0;while(s&k){k<<=1;ans+=k;}return ans;
}void solve()
{cin>>n;mem(dp,-1);int now=0,nex=1,x;for1(__ii,n)//输入n个数{cin>>x;x>>=1;//为简化题目,将16 8 4 2变成 8 4 2 1,最后将答案*2即可。dp[nex][x]=max(dp[nex][x],x);//如果此数作为第一个数for1(s,maxS) if(~dp[now][s])//如果当前状态存在{dp[nex][s]=max(dp[nex][s],dp[now][s]);//不将当前数放入包内int k=lowbit(s),newState,addScore;if(  x>k ) newState=x,addScore=x;else{newState=s+x;addScore= (s&x) ? x+ cal(s,k):x;}dp[nex][newState]=max(dp[nex][newState],dp[now][s]+addScore);}now^=1,nex^=1;}//寻找到答案int ans=0;for1(s,maxS) if(~dp[now][s]) ans=max(ans,dp[now][s]);ans<<=1;//最后还要*2,因为之前所有数都除2了。printf("%d\n",ans);
}
int main()
{std::ios::sync_with_stdio(false);int T,x;cin>>T;while(T--) solve();return 0;
}

ZOJ 3802 Easy 2048 Again 状态压缩dp相关推荐

  1. ZOJ-3802:Easy 2048 Again(2048游戏 状态压缩dp)

    题目链接:点击打开链接 题目大意: 给一些连续的点,从第一个点走到最后一个点,可以选择走或者不走,走的两个点相同则合为新的点,并奖励额外的分数.这个操作是可以一直递推的,例如16,8,4,2. 若下一 ...

  2. 状态压缩dp学习小记part2

    继续学习状态压缩的相关知识. 本来准备继续按照上篇博文里提到的那篇论文继续学习,但被矩形完全覆盖虐了回来,决定先做些其他的题增进理解之后再回来做. Zoj 3471 Most Powerful 题目链 ...

  3. 0x56. 动态规划 - 状态压缩DP(习题详解 × 7)

    目录 Problem A. 最短Hamilton路径 ProblemB. 蒙德里安的梦想 Problem C. Corn Fields Problem D. 小国王 Problem E. 炮兵阵地 P ...

  4. POJ 2411 Mondriaan‘s Dream(最清楚好懂的状压DP讲解)(连通性状态压缩DP)

    poj 2411 Mondriaan's Dream(最清晰的状压DP解析) 闫氏DP大法好 我们这里是一列一列地来,因为是一个棋盘性的状态压缩DP,从哪个方向都一样 摆放的小方格总方案数 等价于 横 ...

  5. BZOJ1688|二进制枚举子集| 状态压缩DP

    Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) ...

  6. hdu1074 状态压缩dp+记录方案

    题意:       给你一些作业,每个作业有自己的结束时间和花费时间,如果超过结束时间完成,一天扣一分,问你把n个作业完成最少的扣分,要求输出方案. 思路:       状态压缩dp,记录方案数的地方 ...

  7. FZU-2218 Simple String Problem(状态压缩DP)

    原题地址: 题意: 给你一个串和两个整数n和k,n表示串的长度,k表示串只有前k个小写字母,问你两个不含相同元素的连续子串的长度的最大乘积. 思路: 状态压缩DP最多16位,第i位的状态表示第i位字母 ...

  8. 《算法竞赛进阶指南》打卡-基本算法-AcWing 91. 最短Hamilton路径:位运算、状态压缩dp、dp

    文章目录 题目解答 题目链接 题目解答 分析: 状态压缩dp是用二进制数来表示状态. 数据范围n = 20, 那么状态总量就是2202^{20}220个状态. 可以按照以下思路去思考: 哪些点被用过 ...

  9. 状态压缩DP AcWing算法提高课 (详解)

    基础课的状态压缩点这里 基础课中 蒙德里安的梦想 属于 棋盘式状态压缩dp,最短Hamilton路径 属于 集合状态压缩dp 1064. 小国王(棋盘式/基于连通性) 这种棋盘放置类问题,在没有事先知 ...

最新文章

  1. ios .a和.framework
  2. exchange 2010备份及恢复
  3. WindowsTime服务设置
  4. 解决“无法找到运行搜索助理需要的一个文件”
  5. php粉丝关注功能,Redis实现用户关注功能
  6. 【GOF23设计模式】迭代器模式
  7. python 调用c++库接口出错
  8. hdu1535 Invitation Cards 最短路
  9. @Autowired与@Resource的差别
  10. android win10驱动安装失败,win10系统小米手机驱动安装失败的解决方法
  11. Linux操作系统知识点总结
  12. MEM专业学位论文选题与研究设计
  13. 阿里云物联网平台基础
  14. i技术会 | 爱奇艺品牌广告算法探索和实践
  15. 最新冰盾DDoS防火墙V9.1 新增防护功能更强大
  16. unsigned char,char ,uchar ,UCHAR的区别
  17. python 登录新浪微博_Python 模拟登录新浪微博
  18. public protected private
  19. flex little know
  20. 程序员 - 算法工程师 日常快捷入口大全

热门文章

  1. Vue 数组/对象数据 更新,但是页面不渲染问题
  2. Python入门(上)
  3. 【报告分享】2021中国智能驾驶核心软件产业研究报告-亿欧智库(附下载)
  4. 评分卡模型开发-基于逻辑回归的标准评分卡实现
  5. 基于U-net神经网络的微血管图像分割
  6. Nginx静态资源概述
  7. SVN: File ‘xxxx‘ is locked in another working copy解决办法
  8. UWPWP8.1 中文网页字符乱码 字符乱码 UTF-8转GBK 解决方法
  9. Android EditText获取焦点后只显示光标不弹出软键盘
  10. ubuntu个人版和server_Ubuntu 系统下载、Server与Desktop、i386 和 amd64之间的区别