简略题意:
有n个花瓶和m朵花,第i个花瓶插着第j朵花的价值是
,问n个花瓶插满的最大价值是多少。
需要注意的是里面有一组偏序关系:
那么两位有序的情况下,求最大值的DP即可。
令dp[i][j]代表第i个花瓶插着第j朵花的最大价值。
。
sgu的题目总是充满坑点,对于这题:
1.注意转移时k的取值范围。
2.注意v[i][j]的取值范围。
3.好好读题,注意偏序关系。
#define others
#ifdef poj
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#endif // poj
#ifdef others
#include <bits/stdc++.h>
#endif // others
//#define file
#define all(x) x.begin(), x.end()
using namespace std;
const double eps = 1e-8;
int dcmp(double x) { if(fabs(x)<=eps) return 0; return (x>0)?1:-1;};
typedef long long LL;
namespace solver {
const int maxn = 110;
int n, m, ans = -1e9;
int v[maxn][maxn];
int dp[maxn][maxn];
pair<int, int> pre[maxn][maxn];
pair<int, int> res;
void solve() {
memset(dp, -0x3f3f3f3f, sizeof dp);
dp[0][0] = 0;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%d", &v[i][j]);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
for(int k = i- 1; k < j; k++) {
int val = dp[i-1][k] + v[i][j];
if(val > dp[i][j]) {
dp[i][j] = val;
pre[i][j] = {i-1, k};
}
}
}
}
res = {n, n};
for(int i = 1; i <= m; i++) {
if(dp[n][i] > dp[res.first][res.second]) {
res = {n, i};
}
}
cout<<dp[res.first][res.second]<<endl;
vector<int> G;
for(int i = 0; i < n; i++) {
G.push_back(res.second);
res = pre[res.first][res.second];
}
reverse(all(G));
for(int i = 0; i < n; i++)
printf("%d%c", G[i], i == n-1?'\n':' ');
}
}
int main() {
#ifdef file
freopen("gangsters.in", "r", stdin);
freopen("gangsters.out", "w", stdout);
#endif // file
// int t;
// scanf("%lld", &t);
// while(t--)
solver::solve();
return 0;
}
/*
3 5
-7 -23 -5 -24 -16
-5 -21 -4 -10 -23
-21 -5 -4 -20 -20
*/