-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPoj1390.cpp
57 lines (55 loc) · 1.53 KB
/
Poj1390.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cstring>
using namespace std;
struct Block
{
int color;
int len;
};
Block segment[200];
int score[200][200][200];
int click_box(int start, int end, int extra_len)
{
if (score[start][end][extra_len] > 0)
return score[start][end][extra_len];
int result = segment[end].len + extra_len;
result *= result;
if (start == end) {
score[start][end][extra_len] = result;
return score[start][end][extra_len];
}
result += click_box(start, end - 1, 0);
for (int i = end - 1; i >= start; i--) {
if (segment[i].color != segment[end].color)
continue;
int temp = click_box(start, i, segment[end].len + extra_len) + click_box(i + 1, end - 1, 0);
result = max(result, temp);
}
score[start][end][extra_len] = result;
return score[start][end][extra_len];
};
int main()
{
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int n, end = 0; // end means the number of blocks, and begins from zero.
cin >> n;
segment[end].len = 1;
cin >> segment[end].color;
for (int j = 1; j < n; j++) {
int color;
cin >> color;
if (color == segment[end].color)
segment[end].len++;
else {
end++;
segment[end].color = color;
segment[end].len = 1;
}
}
memset(score, 0, sizeof(score));
cout << "Case " << i + 1 << ": " << click_box(0, end, 0) << endl;
}
return 0;
}