[UVA][dp] 10559 - Blocks@Morris' Blog|PChome Online 人新台
2014-01-24 11:14:57| 人4,729| 回0 | 上一篇 | 下一篇

[UVA][dp] 10559 - Blocks

0 收藏 0 0 站台

Problem A

Blocks

Input: Standard Input

Output: Standard Output

Time Limit: 10 Seconds

 

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 ofthis 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                             Output for Sample Input

2

9

1 2 2 2 2 3 3 3 1

1

1

Case 1: 29

Case 2: 1


Problemsetter: Rujia Liu, Member of Elite Problemsetters' Panel

Special Thanks: Cailiang Liu & Rongjing Xiang from IOI2003 China National Training Team


目描述:

一空的消去,每次消去都近相同的消去,且剩的合。

消去得的分的平方。

求最大得分。

目解法:

// 相地,非常有挑性。

1. 先入成的段色 A[], B[] // A : 色, B :

2. 考最左的段要消是不消,如果不消的,另一串接之後消去。

dp[i][j][k] 表示段 [i, j] 左接了 k A[i] 相同色的最大得分。

3. 如果消去最左,得分 = (B[i] + k)^2 + dp[i+1][j][0]

4. 如果不消的,得分 = max(dfs(i+1, pos-1, 0) + dfs(pos, j, k + B[i]))

A[pos] == A[i] // 色相同的那一串接在一起消去。

#include <stdio.h>
#include <algorithm>
using namespace std;
int A[205], B[205];
int dp[205][205][205]; //[l][r][___{l, r}]
char used[205][205][205] = {}, usedcases = 0;
int dfs(int l, int r, int sl) {
    if(l > r)    return 0;
    if(used[l][r][sl] == usedcases)
        return dp[l][r][sl];
    int &ret = dp[l][r][sl];
    ret = 0;
    used[l][r][sl] = usedcases;
    ret = dfs(l+1, r, 0) + (B[l] + sl)*(B[l] + sl);
    for(int i = l+1; i <= r; i++) {
        if(A[i] == A[l]) {
            ret = max(ret, dfs(l+1, i-1, 0) + dfs(i, r, sl + B[l]));
        }
    }
    return ret;
}
int main() {
    int testcase, n, m;
    int i, j, k, cases = 0;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d", &n);
        for(i = 0; i < n; i++)
            scanf("%d", &A[i]);
        for(i = 1, B[0] = 1, m = 0; i < n; i++) {
            if(A[m] == A[i])
                B[m]++;
            else {
                A[++m] = A[i], B[m] = 1;
            }
        }
        usedcases++;
        printf("Case %d: %d\n", ++cases, dfs(0, m, 0));
    }
    return 0;
}
/*
2
9
1 2 2 2 2 3 3 3 1
1
1
*/

台: Morris
人(4,729) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 教育(修、留、研究、教育概) | 人分: UVA |
此分下一篇:[UVA] 10535 - Shooter
此分上一篇:[UVA] 10504 - Hidden squares

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86