【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

思路

爆搜,算一下前 \(x-1\) 全部为0时前面的方案数为 \(sum\) 时后面的情况。

然后就是转移。

还有就是,long long会TLE,要用int。

总结

这道题我一开始想的是dp,后来觉得是dp写成记忆化搜索,看了题解才发现直接写爆搜也行。

对于这种题,第一个反应很容易是dp,但事实上在这类题中由于数据范围过小,甚至能搜索过,也启示着我在类似题中不必执着于写dp,可以写搜索。

Code

// Problem: CF6D Lizards and Basements 2
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF6D
// Memory Limit: 62 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
// #define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
#define N 200
int n, m, i, j, k; 
int h[N], ansx[N], now[N]; 
int a, b, ans=0x7fffffff; 

void dfs(int x, int sum)
{
	// printf("%lld %lld\n", x, sum); 
	if(sum>=ans) return ; 
	if(x==n)
	{
		if(h[x]<0) 
		{
			ans=sum; 
			for(int i=1; i<=ans; ++i) ansx[i]=now[i]; 
		}
		return ; 
	}
	for(int i=0; i<=max({h[x-1]/b, h[x]/a, h[x+1]/b})+1; ++i)
	{
		if(h[x-1]-i*b>=0) continue; 
		for(int j=1; j<=i; ++j) now[++sum]=x; 
		h[x-1]-=i*b; h[x+1]-=i*b; h[x]-=i*a;
		dfs(x+1, sum); 
		h[x-1]+=i*b; h[x+1]+=i*b; h[x]+=i*a; 
		for(int j=1; j<=i; ++j) now[sum--]=0; 
	}
}

signed main()
{
//	freopen("tiaoshi.in", "r", stdin); 
//	freopen("tiaoshi.out", "w", stdout); 
	n=read(); a=read(); b=read(); 
	for(i=1; i<=n; ++i) h[i]=read(); 
	dfs(2, 0); 
	printf("%d\n", ans); 
	for(i=1; i<=ans; ++i) printf("%d ", ansx[i]); 
	return 0; 
}

posted @ 2022-01-10 16:10  zhangtingxi  阅读(43)  评论(0编辑  收藏  举报