其他分享
首页 > 其他分享> > 洛谷 P1854 花店橱窗布置

洛谷 P1854 花店橱窗布置

作者:互联网

花店橱窗布置

里程碑式的一题!

第一次完全未看题解做出的一道dp题


Code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
//Mystery_Sky
//
#define M 500
#define INF 0x3f3f3f3f
int f[M][M], a[M][M];
int n, m;

void print(int x, int y)
{
    if(x == 0) return;
    for(int i = 1; i <= y; i++) {
        if(f[x][y] - a[x][i] == f[x-1][i-1]) {
            print(x-1, i-1);
            printf("%d ", i);
            break;
        }
    }
    return;
}

int main() {
    scanf("%d%d", &n, &m);
    memset(f, -INF, sizeof(f));
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            scanf("%d", &a[i][j]);
        }
    }
    f[0][0] = 0;
    for(int i = 1; i <= m; i++) f[1][i] = max(f[1][i-1], a[1][i]), f[0][i] = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = i; j <= m; j++) {
            for(int k = i; k <= j; k++) {
                f[i][j] = max(f[i][j], f[i-1][k-1] + a[i][k]);              
            }
        }
    }
    printf("%d\n", f[n][m]);
    print(n, m);
    return 0;
}

标签:洛谷,橱窗,花店,int,布置,include,P1854,define
来源: https://www.cnblogs.com/Benjamin-cpp/p/11031080.html