Luogu P1854 花店橱窗布置

裸的DP,难度标签要改

\(f[i][j]\) 表示前\(i\)种花且第\(i\)种花选第\(j\)个花盆的最优解

\(a[i][j]\) 表示第\(i\)种花选第\(j\)个花盆的美学值的话

状态转移方程:

\(f[i][j] = max(f[i-1][k](k<j))+a[i][j]\)

#include<bits/stdc++.h>
using namespace std;
char ch;
int fh = 1;
inline int redn(int &ret) {
	ch = getchar(),fh = 1;
	while(ch<'0'||ch>'9') {if(ch == '-') fh = -1;ch = getchar();}
	while(ch>='0'&&ch<='9') {ret = ret*10+ch-'0';ch = getchar();}
	return ret = ret*fh;
}
int F,V;
int a[233][233],f[233][233],flag[233][233];   //f---DP数组,flag---flag[i][j]表示在f[i][j]的最优状态下第i-1种花选择的花盆
inline void print(int d,int x) {
    if(d == 1) return ;
    print(d-1,flag[d][x]);
    printf("%d ",flag[d][x]);
}
int main() {
	redn(F),redn(V);
	for(int i=1;i<=F;++i)
		for(int j=1;j<=V;++j) redn(a[i][j]);
    for(int i=1;i<=F;++i) 
        for(int j=1;j<=V;++j) f[i][j] = -((int)1e9+7);
    for(int i=1;i<=V;++i) f[1][i] = a[1][i],flag[1][i] = i;
    for(int i=2;i<=F;++i) {
	for(int j=1;j<=V;++j) {
		for(int k=1;k<j;++k) {
			if(f[i][j] != max(f[i-1][k],f[i][j])) {
                                   f[i][j] = max(f[i-1][k],f[i][j]);
                                   flag[i][j] = k;
                               }
                       }
		f[i][j] += a[i][j];
	}
    }
    int ans = -((int)1e9+1e5),k;
    for(int i=1;i<=V;++i) if(ans<f[F][i]) {ans = f[F][i];k = i;}
	printf("%d\n",f[F][k]);
    print(F,k);printf("%d",k);
	return 0;
}
// f[i][j]表示前i种花且第i种花选第j个花盆的最优解
posted @ 2020-01-27 12:29  AxDea  阅读(125)  评论(0编辑  收藏  举报