水题笔记_ luogu1854 花店橱窗布置 [背包DP]

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

懵逼的 题目

传送至 Luogu

扯淡的 题解

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

沙茶的 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#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