[CF6D]Lizards and Basements 2 题解

[CF6D]Lizards and Basements 2 题解

题目地址:Codeforces:Problem – 6D – Codeforces、洛谷:CF6D Lizards and Basements 2 – 洛谷 | 计算机科学教育新生态

题目描述

This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced.

Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) — they lose b (1 ≤ b < a ≤ 10) health points each.

As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball.

The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies?

Polycarp can throw his fire ball into an archer if the latter is already killed.

题意简述

有一排共$n$个怪物从左到右依次编号,你可以攻击编号为$2 \sim n-1$的怪物,一次攻击会对攻击怪物造成$a$点伤害,对该怪物两边的怪物造成$b$点伤害,已知每个怪物的生命值,怪物生命值小于0时视为已击败,求击败所有怪物的最少攻击次数及方案。

输入输出格式

输入格式:
The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10). The second line contains a sequence of n integers — h1, h2, …, hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has.

输出格式:
In the first line print t — the required minimum amount of fire balls.
In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order.

输入输出样例

输入样例#1:

3 2 1
2 2 2

输出样例#1:

3
2 2 2 

输入样例#2:

4 3 1
1 4 1 1

输出样例#2:

4
2 2 3 3 

题解

容易发现的性质是,一个位置上的伤害值只取决于本身、左边和右边三个位置上的攻击次数。考虑动态规划解决本题,令$f(i, j, k)$表示考虑到怪物$i$,且怪物$i$被攻击了$k$次、怪物$i-1$被攻击了$j$次,把$[1, i-1]$内的怪物全部击败的攻击最少次数,由于一定要击败怪物$i$,枚举攻击怪物$i$的次数转移即可,即
$$ f(i, j, k) = \min_{bm+aj+bk>h_{i-1}}\{ f(i-1, m, j) + k \} $$

由于我们不能攻击$n$怪物,这个DP只能最多做到第一维为$n-1$的位置,剩下的需要我们自行判断是否增加几次攻击把怪物$n$击败。只需要在0、击败怪物$n-1$的最少次数、击败怪物$n$的最少次数里取一个max就可以了。

代码在不断的改动中写的比较丑,就凑合着看吧。复杂度$O(nk^3)$,其中$k$指每个位置攻击次数的可能最大值,例如代码中取了$200$。

代码

// Code by KSkun, 2019/8
#include <cstdio>
#include <cctype>
#include <cstring>

#include <algorithm>

typedef long long LL;

inline char fgc() {
	static char buf[100000], * p1 = buf, * p2 = buf;
	return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
		? EOF : *p1++;
}

inline LL readint() {
	LL res = 0, neg = 1; char c = fgc();
	for(; !isdigit(c); c = fgc()) if(c == '-') neg = -1;
	for(; isdigit(c); c = fgc()) res = res * 10 + c - '0';
	return res * neg;
}

inline char readsingle() {
	char c;
	while(!isgraph(c = fgc())) {}
	return c;
}

const int MAXN = 20;

int n, a, b, h[MAXN], f[MAXN][205][205], prei[MAXN][205][205];

int main() {
	n = readint(); a = readint(); b = readint();
	for(int i = 1; i <= n; i++) {
		h[i] = readint() + 1;
	}
	memset(f, 0x3f, sizeof(f));
	for(int i = 0; i <= 200; i++) {
		if(i * b >= h[1]) f[2][0][i] = i;
	}
	for(int i = 3; i < n; i++) {
		for(int j = 0; j <= 200; j++) {
			for(int k = 0; k <= 200; k++) {
				for(int ii = 0; ii <= 200; ii++) {
					if(ii * b + j * a + k * b >= h[i - 1] && f[i][j][k] > f[i - 1][ii][j] + k) {
						f[i][j][k] = f[i - 1][ii][j] + k;
						prei[i][j][k] = ii;
					}
				}
			}
		}
	}
	int ans = 2e9, ai, aj;
	for(int i = 0; i <= 200; i++) {
		for(int j = 0; j <= 200; j++) {
			int res = f[n - 1][i][j] +
				std::max({0, (int)ceil(double(h[n - 1] - i * b - j * a) / a), (int)ceil(double(h[n] - j * b) / b)});
			if(ans > res) {
				ans = res; ai = i; aj = j;
			}
		}
	}
	printf("%d\n", ans);
	for(int i = 1; i <= ans - f[n - 1][ai][aj]; i++) {
		printf("%d ", n - 1);
	}
	int ni = ai, nj = aj, nn = n - 1;
	while(nn != 1) {
		for(int i = 1; i <= nj; i++) printf("%d ", nn);
		int onj = nj; nj = ni; ni = prei[nn][ni][onj]; nn--;
	}
	return 0;
}


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据