普通的带输出背包…负数让结果变得扑朔迷离

懵逼的 题目

传送至 Luogu

扯淡的 题解

因为美学有负数而且每朵小发发都要放…所以…
不过我好想还是不习惯输出方案的dp呢…

沙茶的 代码

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#define MAXN (100 + 5)
#define INF (0x7ffffff)
using namespace std;
int fn, bn;
int ans = 0;
int f[MAXN][MAXN];
int pr[MAXN][MAXN];
int a[MAXN][MAXN];
int ansb;
void dp();
void print(int, int);
int main()
{
    for (int i = 1; i < MAXN; i++)
    {
        for (int j = 1; j < MAXN; j++)
        {
            f[i][j] = -INF;
        }
    }
    scanf("%d%d", &fn, &bn);
    for (int i = 1; i <= fn; i++)
    {
        for (int j = 1; j <= bn; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
    dp();
    printf("%d\n", ans);
    print(fn, ansb);
    return 0;
}
void dp()
{
    for (int i = 1; i <= fn; i++)
    {
        for (int j = i; j <= bn; j++)
        {
            for (int k = i - 1; k < j; k++)
            {
                if (f[i][j] == -INF)//这里可以保证一定会选一个
                {
                    f[i][j] = f[i - 1][k] + a[i][j];
                    pr[i][j] = k;
                }
                if (f[i][j] < (f[i - 1][k] + a[i][j]))
                {
                    f[i][j] = f[i - 1][k] + a[i][j];
                    pr[i][j] = k;
                }
            }
        }
    }
    for (int i = fn; i <= bn; i++)
    {
        if (ans < f[fn][i])
        {
            ans = f[fn][i];
            ansb = i;
        }
    }
}
void print(int dqf, int dqb)
{
    if (dqf != 1)
    {
        print(dqf - 1, pr[dqf][dqb]);
    }
    printf("%d ", dqb);
}

By 特别菜的 Cansult