SGU 104 Little shop of flowers [dp]
http://acm.sgu.ru/problem.php?contest=0&problem=104
题意:V个花盆排成一列,有F束花,现给出a[i][j]表示第i束花放在第j个花盆的美观度。要把F束花都放到盆里,编号小的花必须放到编号大的花的前面,每个盆最多放一束,问美观度的最大和是多少,还要输出每束花放的花盆编号。
d[i][j]表示i号花放在j号花盆的最大美观度。则d[i][j] = max { d[i-1][k] | i-1 <= k <= j-1 } + a[i][j]
#include<iostream> #include<cstdio> #include<vector> using namespace std; #define rep(i,f,t) for(int i = (f),_end = (t); i <= _end; ++i) const int maxn = 110; int a[maxn][maxn]; int d[maxn][maxn]; int prev[maxn][maxn]; int main(){ int f,v; scanf("%d%d",&f,&v); rep(i,1,f)rep(j,1,v){ scanf("%d",&a[i][j]); } int ansj = f; rep(i,1,f)rep(j,i,v-f+i){ d[i][j] = -11111111; rep(k,i-1,j-1){ if(d[i][j] < d[i-1][k]+a[i][j]){ d[i][j] = d[i-1][k] + a[i][j]; prev[i][j] = k; } } if(i == f){ if(d[i][ansj] < d[i][j]){ ansj = j; } } } vector<int> o; printf("%d\n",d[f][ansj]); for(int i = f; i > 0; --i){ o.push_back(ansj); ansj = prev[i][ansj]; } while(!o.empty()){ printf("%d",o.back()); o.pop_back(); printf("%c",(o.empty() ? '\n' : ' ')); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。