codeforces 6D
2 seconds
64 megabytes
standard input
standard output
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.
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.
3 2 1
2 2 2
3
2 2 2
4 3 1
1 4 1 1
4
2 2 3 3
(万万没想到啊,这题竟然可以用深搜。。我满脑子想的都是纯暴力,又是一道看了题解才恍然大悟的题
题意:n个人站成一排,发火球攻击,选择攻击的人受到a点伤害,其相邻的人受到b点伤害,目的是让所有人的血量都小于0,求最小攻击次数。
解题思路:因为火球只能攻击2-n-1,所以搜索的判断条件是前一个人的血量得小于0,结束条件是n的血量小于0。其实知道是搜索后,就挺好想的了
我的ac代码:
1 #include <iostream>
2 #include <cstdio>
3 #include <map>
4 using namespace std;
5 const int maxn = 22222;
6 int nu[maxn],ans=0x3f3f3f3f;
7 int res[maxn],num[maxn];
8
9 int n,a,b,len=0;
10 void dfs(int x,int c) {
11 if(c>=ans) return ;
12 if(x==n) {
13 if(nu[x]<0){
14 ans=c;
15 for(int k=0;k<ans;++k) {
16 res[k]=num[k];
17 }
18 }
19 return ;
20 }
21 for(int j=0;j<=max(nu[x-1]/b+1,max(nu[x]/b+1,nu[x+1]/b+1));++j) {
22 if(nu[x-1]<b*j) {
23 nu[x-1]-=b*j;
24 nu[x]-=a*j;
25 nu[x+1]-=b*j;
26 for(int k=0;k<j;++k) {
27 num[len++]=x;
28 // cout<<i<<endl;
29 }
30 dfs(x+1,c+j);
31 len-=j;
32 nu[x-1]+=b*j;
33 nu[x]+=a*j;
34 nu[x+1]+=b*j;
35 }
36 }
37
38 }
39 int main() {
40 ios::sync_with_stdio(false);
41 cin.tie(0);cout.tie(0);
42 cin>>n>>a>>b;
43 for(int i=1;i<=n;++i) {
44 cin>>nu[i];
45 }
46 dfs(2,0);
47 cout<<ans<<endl;
48 for(int i=0;i<ans;++i) cout<<res[i]<<" ";
49 return 0;
50 }
高手ac代码:(vector真是好啊
1 #include<bits/stdc++.h>
2 using namespace std;
3 int ans=9999999;
4 int h[100];
5 int a,b,n;
6 vector<int>V;
7 vector<int>V2;
8 void dfs(int x,int times)
9 {
10 if(times>=ans)return;
11 if(x==n)
12 {
13 if(h[x]<0){
14 V2=V;
15 ans=times;
16 }
17 return ;
18 }
19 for(int i=0; i <= max( h[x-1]/b+1,max( h[x]/a+1, h[x+1]/b+1) );i++)
20 {
21 if(h[x-1]<b*i)
22 {
23 h[x-1] -= b*i;
24 h[x] -= a*i;
25 h[x+1] -= b*i;
26 for(int j=0;j<i;j++) V.push_back(x);
27 dfs(x+1,times+i);
28 for(int j=0;j<i;j++) V.pop_back();
29 h[x-1] += b*i;
30 h[x] += a*i;
31 h[x+1] += b*i;
32 }
33 }
34 }
35 int main()
36 {
37 cin>>n>>a>>b;
38 for(int i=1;i<=n;i++)cin>>h[i];
39 dfs(2,0);
40 cout<<ans<<endl;
41 for(int i=0;i<V2.size();i++)cout<<V2[i]<<" ";
42 cout<<endl;
43 return 0;
44 }
最新文章
- 金融行业的BI应用分析
- Swift实战-豆瓣电台(六)视图跳转,传参及回跳
- 代码中特殊的注释技术&mdash;&mdash;TODO、FIXME和XXX的用处
- nginx 一般网站部署常用参数设置
- 十六进制转十进制 - C
- Qt5.0.2无法发布问题
- java学习之面向对象概念
- VS2012 利用正则统计项目代码行数
- C#(Net)软件开发常用工具汇总,提高你的开发效率
- Android开发_关于点击事件
- 关于Linux系统清理/tmp/文件夹的原理
- Linux基础命令---压缩与打包
- tp框架为什么验证码加载不出来?----- ob_clean() 可解决
- 开发vue但不使用vue-cli和webpack相关注意事项
- 详解ASP.NET Core API 的Get和Post请求使用方式
- 从数据库取出两个同样的字符串用equals比较返回false
- urllib.parse.quote
- [UE4]多播代理
- apache ftp server的简单入门(properties验证)
- 精通Python爬虫-03-狩猎大师