POJ 1157
简单的dp。状态定义好就迎刃而解,令\(dp(i, j)\)为第\(i\)种花必须在第j个花瓶,且满足条件的最大aesthetic value。
状态转移方程也很好想,估计了下时间复杂度发现没问题,就着手实现了
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <map>
#include <set>
using namespace std;
const int maxf= 105;
const int maxv= 105;
const int INF= 0x3f3f3f3f;
int fv[maxf][maxv], dv[maxf][maxv];
int f, v;
int main(int argc, char const *argv[])
{
scanf("%d %d", &f, &v);
for (int i= 1; i<= f; ++i){
for (int j= 1; j<= v; ++j){
scanf("%d", fv[i]+j);
}
}
memset(dv, 0xc0, sizeof(dv));
for (int i= 0; i<= v; ++i){
dv[0][i]= 0;
}
for (int i= 1; i<= f; ++i){
dv[i][i]= dv[i-1][i-1]+fv[i][i];
}
int ud= v-f;
for (int d= 1; d<= ud; ++d){
for (int i= 1, j= i+d; j<= v; ++i, ++j){
int tv= dv[i][j];
for (int k= i-1; k< j; ++k){
tv= max(tv, dv[i-1][k]);
}
dv[i][j]= tv+fv[i][j];
}
}
int ans= -INF;
for (int i= f; i<= v; ++i){
ans= max(ans, dv[f][i]);
}
printf("%d\n", ans);
return 0;
}