Poj 1390 blocks (elimination of DP blocks in the classic interval)

Source: Internet
Author: User
Blocks
Time limit:5000 Ms   Memory limit:65536 K
Total submissions:4250   Accepted:1704

Description

Some of you may have played a game called 'blocs '. there are n blocks in a row, each box has a color. here is an example: gold, silver, bronze, gold.
The corresponding picture will be as shown below:

Figure 1
If some adjacent boxes are all of the same color, and both the box to its left (if it exists) and its right (if it exists) are of some other color, we call it a 'box segment '. there are 4 Box segments. that is: gold, silver, bronze, gold. there are 1, 4, 3, 1 box (es) in the segments respectively.

Every time, you can click a box, then the whole segment containing that box disappears. if that segment is composed of K boxes, you will get K * k points. for example, if you click on a silver box, the silver segment disappears, you got 4*4 = 16 points.

Now let's look at the picture below:

Figure 2

The first one is optimal.

Find the highest score you can get, given an initial state of this game.

Input

The first line contains the number of tests T (1 <= T <= 15 ). each case contains two lines. the first line contains an integer N (1 <= n <= 200), the number of boxes. the second line contains N integers, representing the colors of each box. the integers are in the range 1 ~ N.

Output

For each test case, print the case number and the highest possible score.

Sample Input

291 2 2 2 2 3 3 3 111

Sample output

Case 1: 29Case 2: 1

Source

Liu [email protected]


Question:

A row of colored squares can eliminate blocks with the same adjacent colors each time. The score is the square of the number of squares. After elimination, the remaining squares are merged. Ask how to eliminate the squares to maximize the total score.


Ideas:

The original black book question (p123) is combined with the original adjacent same blocks to obtain the color array C and the corresponding length Len, DP [I] [J] [k] For I ~ J interval, which is used to eliminate the maximum score (of course, the color of K blocks must be the same as that of J). How can we eliminate the Len [J] and K segments, there are two possibilities:

1. Remove separately, DP [I] [J] [k] = DP [I] [J-1] [0] + (LEN [J] + k) ^ 2;

2. if the last block to be removed is P, so DP [I] [J] [k] = DP [I] [p] [K + Len [J] + dp [p + 1] [J-1] [0].

It can be optimized based on the same color and K range of P and J.


Code:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define maxn 205#define MAXN 200005#define INF 0x3f3f3f3f#define mod 1000000007#define eps 1e-6const double pi=acos(-1.0);typedef long long ll;using namespace std;int n,m,ans,tot;int a[maxn],c[maxn],len[maxn],pos[maxn],last[maxn];int dp[205][205][205],num[maxn][maxn];void solve(){    int i,j,k,p;    memset(pos,0,sizeof(pos));    for(i=1;i<=tot;i++)    {        last[i]=pos[c[i]];        pos[c[i]]=i;    }    memset(num,0,sizeof(num));    for(i=tot;i>=1;i--)    {        for(j=1;j<=n;j++)        {            if(j==c[i]) num[j][i]=num[j][i+1]+len[i];            else num[j][i]=num[j][i+1];        }    }    memset(dp,0,sizeof(dp));    for(int l=1;l<=tot;l++)    {        for(i=1;i<=tot;i++)        {            j=i+l-1;            if(j>tot) break ;            for(k=0;k<=num[c[j]][j+1];k++)            {               dp[i][j][k]=dp[i][j-1][0]+(len[j]+k)*(len[j]+k);               for(p=last[j];p>=i;p=last[p])               {                   dp[i][j][k]=max(dp[i][j][k],dp[i][p][len[j]+k]+dp[p+1][j-1][0]);               }            }        }    }    ans=dp[1][tot][0];}int main(){    int i,j,test,ca=0;    scanf("%d",&test);    while(test--)    {        scanf("%d",&n);        for(i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        tot=0;        memset(len,0,sizeof(len));        for(i=1;i<=n;)        {            tot++;            c[tot]=a[i];            while(i<=n&&a[i]==c[tot]) i++,len[tot]++;        }        solve();        printf("Case %d: %d\n",++ca,ans);    }    return 0;}


Poj 1390 blocks (elimination of DP blocks in the classic interval)

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.