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; }
|