Easy 2048 Again-zoj 3802 like indentation DP

Source: Internet
Author: User

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 the area, he cannot come back again), there is so Me underground treasures on all area of the road which have 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 was similar to the game Flappy 2048.

Dark_sun ' s bag is the like a queue. When he gets a treasure, the treasure'll is pushed back into the end of the bag. The score would add the value of the treasure. For example, when there is treasures on the road in the order of {2, 8, 4, 8} and if Dark_sun decides-dig all of them, He'll get the final score of 2+8+4+8=22. And his bag would finally become the sequence of {2, 8, 4, 8}.

if the adjacent treasures in the Dark_sun ' s bag has the same value, they would mix into a bigger treasure which has the value of their sum (the double value of the Previous one). And Dark_sun would 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'll get the basic score of 18 (2+8+8). And when the last treasure (value 8) was pushed back to his bag, his bag would turn {2, 8, 8} into {2, +} and he would get A bonus score of 16. And his final score would become 18+16=34 (which is the best strategy in this case.)

Notice the treasures mix to the bigger one automatically when there is the same adjacent treasures. For example, when there is treasures of {2, 2, 4, 8, up} on the road, and if Dark_sun decides-dig all of them, he'll Get the basic score of (2+2+4+8+16) and a bonus score of 60 (4+8+16+32). At last he'll get the total score and the bag becomes {32}.

Now, Dark_sun wants-to-get the highest score (no matter what's the treasure in his bag), can-tell-him the what ' s the H Ighest score?

Input

The first line n is a integer, which is the case number. In each case, the first line L is a integer, which is the length of the road. ( 0 < L ≤500) The second line contains L integers which can is only 2, 4, 8 or. This means the value of treasure in each area of the road.

Output

For each case, you should the 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 would choose {8,4,4,8,4,2,2}. Firstly, the first three treasures would be a combined to + and then the {16,8,4,2,2} would become 32. And he'll get the basic score (8+4+4+8+4+2+2) and the bonus score 84 (8+16+4+8+16+32).


Test instructions: For a sequence, you can select or not select some numbers sequentially. And put it in a stack. Let's say that the two numbers together will merge, your score is the sum of the combined numbers, and the last score is the most.

Idea: Pressure DP, the problem itself is binary, it is convenient, note to use scrolling array. Otherwise it will explode the MLE.

Here you use a scrolling array to record the pressure and score before you select the nth number.

The AC code is as follows:

#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;  Do not select this number, retain the original pressure situation Match[pos2][s]=max (match[pos2][s],match[pos][s]); if ((s%k)!=0)//Select this number. And the previous pressure situation has a number smaller than K. For example, before the result of the pressure is 8 4 2 then you add a 4, then the queue will be 8 4 2 4,//The first three numbers can not be merged again.    So the result of the pressure is only to retain K to be able to {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)//merge the same number {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; Number of cases before for (j=1;j<=f[0][a];j++)//f[0][a] record if (Vis[f[j][a]]!=pos) {solve (f[j][a],k,a,b);//Before the result of the pressure is f[j][a       ] Consider choosing or not selecting the result of this number vis[f[j][a]]=pos;  }} printf ("%lld\n", ans); }}



Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Easy 2048 Again-zoj 3802 like indentation DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.