设dp[i][k],表示前i个站台,在第i到第i+1个站台之间查票,一共查了k次票的最大人数。
那么有dp[i][k] = max{dp[j][k - 1] + w},0 <= j < i
其中w = ∑A[x][y],j < x <= i,i < y <= n。
把w在二维平面里画出来,刚好是A的后缀和。
然后就可以转移了。
行末换行的写法是学习的PoPoQQQ的(%%%)
/* Pigonometry */
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 605, maxm = 55;
int n, m, sum[maxn][maxn], dp[maxn][maxm], pre[maxn][maxm], way[maxm];
inline int iread() {
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}
inline int calc(int x1, int y1, int x2, int y2) {
return sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1];
}
int main() {
n = iread(); m = iread();
for(int i = 1; i < n; i++) for(int j = i + 1; j <= n; j++)
sum[i][j] = iread();
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++)
sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
memset(dp, -0x3f, sizeof(dp));
dp[0][0] = 0;
int ans = 0;
for(int k = 1; k <= m; k++) for(int i = 1; i < n; i++) {
for(int j = 0; j < i; j++) {
int w = calc(j + 1, i + 1, i, n);
if(dp[i][k] < dp[j][k - 1] + w) {
dp[i][k] = dp[j][k - 1] + w;
pre[i][k] = j;
}
}
if(k == m && dp[i][k] > ans) {
ans = dp[i][k];
way[0] = i;
}
}
for(int i = m, j = way[0]; i; i--) {
way[i] = j;
j = pre[j][i];
}
for(int i = 1; i <= m; i++) printf("%d%c", way[i], " \n"[i == m]);
return 0;
}