Blocks
| Time limit:5000 Ms |
|
Memory limit:65536 K |
| Total submissions:4173 |
|
Accepted:1661 |
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
Given n squares, some of them are continuous. Each time you click a square, you can remove the squares of the same color that are consecutively different from each other and earn points to eliminate the square of the length. Given a sequence of squares, calculate the maximum number of points that can be obtained. Question: the state equation score [I] [J] [k] counts continuous small blocks from the I TO THE J blocks, and K consecutive records follow the J blocks. the maximum point obtained by the same color. I think the problematic code is modeled like the handout code, but it can also be AC, and the time consumption is 344 Ms. It should be something I didn't understand:
#include <stdio.h>#include <string.h>#define maxn 200struct 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;}
I think there is no problem with the code, and the time consumption is 1688 MS:
#include <stdio.h>#include <string.h>#define maxn 200struct 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;}