标签:back show name new turn c++ ace scanf for
[题目链接]
https://www.luogu.org/problemnew/show/P1854v
[算法]
f[i][j]表示放了前i束花,第i束花放在第j个花瓶中,所能获得的最大美学值
由于要输出方案,我们不妨对于每个状态记一个pre,最后沿着pre数组反推出方案即可
[代码]
#include<bits/stdc++.h> using namespace std; #define MAXF 110 const int INF = 2e9; int i,j,k,now,pos,F,V; int a[MAXF][MAXF],f[MAXF][MAXF],pre[MAXF][MAXF]; vector< int > path; int main() { scanf("%d%d",&F,&V); for (i = 1; i <= F; i++) { for (j = 1; j <= V; j++) { scanf("%d",&a[i][j]); } } for (i = 1; i <= F; i++) { for (j = 0; j <= V; j++) { f[i][j] = -INF; } } for (i = 1; i <= F; i++) { for (j = 1; j <= V; j++) { for (k = j - 1; k >= 0; k--) { if (f[i - 1][k] + a[i][j] > f[i][j]) { f[i][j] = f[i - 1][k] + a[i][j]; pre[i][j] = k; } } } } pos = 1; for (i = 2; i <= V; i++) { if (f[F][i] > f[F][pos]) pos = i; } printf("%d\n",f[F][pos]); now = F; while (now) { path.push_back(pos); pos = pre[now][pos]; now--; } reverse(path.begin(),path.end()); for (i = 0; i < path.size() - 1; i++) printf("%d ",path[i]); printf("%d\n",path[path.size() - 1]); return 0; }
标签:back show name new turn c++ ace scanf for
原文地址:https://www.cnblogs.com/evenbao/p/9349632.html