poj 1390 区间dp

Blocks
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5035   Accepted: 2065

Description

Some of you may have played a game called 'Blocks'. There are n blocks in a row, each box has a color. Here is an example: Gold, Silver, Silver, Silver, Silver, Bronze, Bronze, 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

2
9
1 2 2 2 2 3 3 3 1
1
1

Sample Output

Case 1: 29
Case 2: 1

Source

 
题意:N个方盒(box)摆成一排,每个方盒有自己的颜色。连续摆放的同颜色方盒构成 一个方盒片段(box segment)玩家每次点击一个方盒,则该方盒所在方盒片段就会消失。若消失的方盒片段 中共有k个方盒,则玩家获得k*k个积分 求获得的最高积分
 
题解:参考pku_gw代码   dp姿势涨
score[i][j][len] 表示在i~j的方盒片段上 片段j右侧有长度为len的且与j颜色相同的片段 获得的最高分
但是左侧的和j颜色相同的怎么处理呢  就需要枚举左侧的片段,判断分块处理j片段更优还是合并处理
更优  递归的终止条件问i==j
 
 1 /******************************
 2 code by drizzle
 3 blog: www.cnblogs.com/hsd-/
 4 ^ ^    ^ ^
 5  O      O
 6 ******************************/
 7 //#include<bits/stdc++.h>
 8 #include<iostream>
 9 #include<cstring>
10 #include<cmath>
11 #include<cstdio>
12 #define ll long long
13 #define mod 1000000007
14 #define PI acos(-1.0)
15 using namespace std;
16 int t;
17 int n;
18 struct node
19 {
20     int color;
21     int len;
22 }se[205];
23 int score[205][205][205];
24 int fun(int l,int r,int len)
25 {
26  if(score[l][r][len]>0)
27         return score[l][r][len];
28  int re=(se[r].len+len);//直接处理右侧
29  re=re*re;
30  if(l==r)
31  {
32      score[l][r][len]=re;
33      return score[l][r][len];
34  }
35  re+=fun(l,r-1,0);
36  for(int j=r-1;j>=l;j--)//枚举左侧片段
37  {
38      if(se[j].color!=se[r].color) continue;
39      int temp=fun(l,j,se[r].len+len)+fun(j+1,r-1,0);//递归 分解 将右侧的合并到左侧
40      if(temp<=re) continue;//判断那个更优
41      re=temp;
42      break;
43  }
44  score[l][r][len]=re;
45  return score[l][r][len];
46 }
47 int main()
48 {
49     scanf("%d",&t);
50     int coun=0;
51     int flag=1;
52     memset(se,0,sizeof(se));
53     while(t--){
54         int coun=0;
55         int exm;
56         scanf("%d",&n);
57         scanf("%d",&se[coun].color);
58         se[coun].len=1;
59         for(int i=1;i<n;i++)//分解片段的过程
60         {
61             scanf("%d",&exm);
62             if(exm==se[coun].color)
63                 se[coun].len++;
64             else
65             {
66                 coun++;
67                 se[coun].color=exm;
68                 se[coun].len=1;
69             }
70         }
71         memset(score,0,sizeof(score));
72         printf("Case %d: %d
",flag++,fun(0,coun,0));
73     }
74     return 0;
75 }
 
原文地址:https://www.cnblogs.com/hsd-/p/5713085.html