Source of BUPT3E 学姐的数列

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
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int f[200][200][16], n;
int main() {
int T, tmp, ans;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
ans = 1;
memset(f,0,sizeof(f));
for (int i = 1; i <= n; ++i) {
scanf("%d", &tmp);
f[i][i][tmp] = 1;
}
for (int i = n-1; i > 0; --i) {
for (int j = i+1; j <= n; ++j) {
f[i][j][0] = max(f[i+1][j][0], f[i][j-1][0]);
for (int t = 1; t <= 15; ++t) {
f[i][j][t] = max(f[i+1][j][t], f[i][j-1][t]);
for (int k = i; k < j; ++k)
if (f[i][k][t-1] && f[k+1][j][t-1])
f[i][j][t] = max(f[i][j][t], f[i][k][t-1] + f[k+1][j][t-1]);
}
}
}
for (int i = 1; i <= 15; ++i)
ans = max(ans, f[1][n][i]);
printf("%d\n", ans);
}
return 0;
}

返回