POJ1390 Blocks 【動態規劃】
阿新 • • 發佈:2018-02-14
data alt ++ his fin tracking out printf box
Blocks
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.?
Time Limit:?5000MS | ? | Memory Limit:?65536K |
Total Submissions:?4173 | ? | Accepted:?1661 |
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.?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
題意:給定n個方塊,當中有些顏色是連續的,每次點擊一個方塊就能夠消除掉跟它連續的同樣的顏色的方塊,獲得積分為消除長度的平方。給定一個方塊序列,求最大能獲得多少積分。
題解:狀態方程score[i][j][k]為將連續小塊統計成大塊後從第i個到第j個慷慨塊且第j個後面有k個連續的與其同色的方塊所獲得的最大積分。
我認為有問題的代碼。仿照著講義代碼寫的。可是也能AC。並且時間消耗是344ms。 應該是那個地方我沒理解:
#include <stdio.h> #include <string.h> #define maxn 200 struct Node{ int color, len; } segment[maxn]; int score[maxn][maxn][maxn], arr[maxn]; int clickBox(int left, int right, int exLen) { if(score[left][right][exLen]) return score[left][right][exLen]; int i, ans, ans2; ans = segment[right].len + exLen; ans = ans * ans; if(left == right) return score[left][right][exLen] = ans; ans += clickBox(left, right - 1, 0); for(i = right - 1; i >= left; --i){ if(segment[i].color != segment[right].color) continue; ans2 = clickBox(left, i, exLen + segment[right].len) + clickBox(i + 1, right - 1, 0); if(ans2 <= ans) continue; ans = ans2; break; } return score[left][right][exLen] = ans; } int main() { int t, n, i, id, cas = 1; scanf("%d", &t); while(t--){ scanf("%d", &n); for(i = 0; i < n; ++i) scanf("%d", arr + i); memset(score, 0, sizeof(score)); segment[id = 0].color = arr[0]; segment[id].len = 1; for(i = 1; i < n; ++i){ if(arr[i] != arr[i-1]){ segment[++id].color = arr[i]; segment[id].len = 1; }else ++segment[id].len; } printf("Case %d: %d\n", cas++, clickBox(0, id, 0)); } return 0; }
我認為沒問題的代碼,時間消耗1688ms:
#include <stdio.h> #include <string.h> #define maxn 200 struct Node{ int color, len; } segment[maxn]; int score[maxn][maxn][maxn], arr[maxn]; int clickBox(int left, int right, int exLen) { if(score[left][right][exLen]) return score[left][right][exLen]; int i, ans, ans2; ans = segment[right].len + exLen; ans = ans * ans; if(left == right) return score[left][right][exLen] = ans; ans += clickBox(left, right - 1, 0); for(i = right - 1; i >= left; --i){ if(segment[i].color != segment[right].color) continue; ans2 = clickBox(left, i, exLen + segment[right].len) + clickBox(i + 1, right - 1, 0); if(ans2 > ans) ans = ans2; } return score[left][right][exLen] = ans; } int main() { int t, n, i, id, cas = 1; scanf("%d", &t); while(t--){ scanf("%d", &n); for(i = 0; i < n; ++i) scanf("%d", arr + i); memset(score, 0, sizeof(score)); segment[id = 0].color = arr[0]; segment[id].len = 1; for(i = 1; i < n; ++i){ if(arr[i] != arr[i-1]){ segment[++id].color = arr[i]; segment[id].len = 1; }else ++segment[id].len; } printf("Case %d: %d\n", cas++, clickBox(0, id, 0)); } return 0; }
POJ1390 Blocks 【動態規劃】