ZOJ 3802 - Easy 2048 Again (状态压缩DP+滚动数组)

21 篇文章 0 订阅

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 Lintegers 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,你可以按顺序取或者不取。

每两个2可以变成4,对答案的贡献也是4,以此类推。取宝藏有贡献,合宝藏也有贡献。

POINT:

可知只有单调递减的时候才有合并宝藏的机会。把每种可能状态压缩。

滚动数组其实就是重复利用这个数组。

参考博客

#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
const int maxn = 111;
const int inf = 0x3f3f3f3f;

int dp[2][9999];
int a[555];

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]);
		int pos=1;
		memset(dp,-1,sizeof dp);
		int ans=0;
		dp[0][0]=0;
		for(int i=1;i<=n;i++){
			for(int j=0;j<=4098*2;j++){
				if(dp[pos^1][j]==-1) continue;
				dp[pos][j]=max(dp[pos][j],dp[pos^1][j]);
				ans=max(dp[pos][j],ans);
				int sum=a[i];
				int t=j;
				if((j&(a[i]-1))==0){
					int to=a[i];
					while(to&j){
						sum+=(to<<1);
						to<<=1;
					}
					t&=~(to-1);
					t|=to;

				}else{
					t=a[i];
				}
				dp[pos][t]=max(dp[pos][t],dp[pos^1][j]+sum);
				ans=max(dp[pos][t],ans);
			}
			pos^=1;
		}
		printf("%d\n",ans);
	}



}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值