Codeforces 6D Lizards and Basements 2

链接

传送门

题意

\(n\)个敌人排成一排,每次可以选择2到\(n-1\)的位置进行攻击,被攻击的位置的敌人减少\(a\)点生命,他两边的位置的敌人减少\(b\)点生命。敌人生命值小于0时会死去。当前位置的敌人死去之后依然可以进行攻击。输出杀死全部敌人的最少攻击次数。

思路

攻击的顺序对结果没有影响,定义状态\(d(i,h1,h2,h3)\)为当前选择位置\(i\),第\(i-1\)\(i+1\)个敌人的生命值依次为\(h1\)\(h2\)\(h3\)。读入数据时对所有生命+1,方便状态表示。当\(h1 \leq 0\)时,可以选择继续攻击位置\(i\)或者,开始攻击位置\(i+1\);其他情况继续攻击位置\(i\)

代码

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
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 12;
const int maxh = 19;
int h[maxn];
int d[maxn][maxh][maxh][maxh];
LL p[maxn][maxh][maxh][maxh];
int ans[maxh * maxh];
#define H [h1][h2][h3]
#define C [c1][c2][c3]
#define ENCODE(i, h1, h2, h3) (((i * 19LL + h1) * 19 + h2) * 19 + h3)
#define DECODE(c) h3 = c % 19, c /= 19, h2 = c % 19, c /= 19, h1 = c % 19, x = c / 19

int main() {
int n, a, b;
scanf("%d%d%d", &n, &a, &b);
for (int i = 1; i <= n; ++i) {
scanf("%d", &h[i]);
++h[i];
}
memset(d, 0x3f, sizeof d);
d[2][h[1]][h[2]][h[3]] = 0;
for (int i = 2; i < n; ++i) {
for (int h1 = h[i - 1]; h1 >= 0; --h1) {
for (int h2 = h[i]; h2 >= 0; --h2) {
for (int h3 = h[i + 1]; h3 >= 0; --h3) {
int& k = d[i]H;
if (k == inf) continue;
LL Code = ENCODE(i, h1, h2, h3);
int c1 = max(0, h1 - b), c2 = max(0, h2 - a), c3 = max(0, h3 - b);
if (k + 1 < d[i]C) {
d[i]C = k + 1;
p[i]C = Code;
}
if (h1 == 0 && k < d[i + 1][h2][h3][h[i + 2]]) {
d[i + 1][h2][h3][h[i + 2]] = k;
p[i + 1][h2][h3][h[i + 2]] = p[i]H;
}
}
}
}
}
printf("%d\n", d[n][0][0][0]);
int tmp = d[n][0][0][0], x = n, h1 = 0, h2 = 0, h3 = 0;
while (tmp != 0) {
LL Code = p[x]H;
DECODE(Code);
ans[tmp--] = x;
}
for (int i = 1; i <= d[n][0][0][0]; ++i) {
printf("%d ", ans[i]);
}
return 0;
}