Hdu3640-I, zombie(模拟+二分)
To simplify the problem ,we provide only one kind of Zombies : normal Zombies with 50 hp each. The map contains one row only, and we provide two kinds of plants : Peashooter (with 10hp) and Potato Mine. The operating steps of every second are as below(Warning: make sure your program strictly follow the order of these steps):1.Put some or none zombies in the right-most grid one by one. (In the right grid of the right-most plant at beginning). 2.Judge every survived zombie, whether he's standing on a grid with a Peashooter. 2.1.If it's true, he attacks the Peashooter in his grid, and the Peashooter decreases 1 hp. The Peashooter's hp may be negative at that moment, but it's still alive! 2.2.If it's false, he moves left for one grid. 3.If there are still some zombies in the map, every survived Peashooter will shoot a pea to the zombie who was put earliest. (the zombie's hp decreases 1 hp for each pea, the zombie's hp can be negative at that moment, but it's still alive!) 4.If there are zombies in the grids where Potato Mine stands , then the Potato Mine explodes , all the zombies' hp in this grid become 0. 5.The plants and zombies with non-positive hp disappear(until now they are dead).
Now, given the map, you are to tell me how many zombies are needed at least, in order to eat the brain in the left-most?
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
const int maxn=;
char S[maxn];
int L,Pnum,Mnum,step;
void init()
{
L=strlen(S)-;
Pnum=Mnum=; //豌豆射手与土豆的个数
for(int i=;i<=L;i++)
if(S[i]=='P') Pnum++;
else Mnum++;
}
bool judge(int totpnum,int pnum,int znum)
{
int nowstep=step;
int P_hp=,Z_hp=; //豌豆射手和僵尸的hp
while(pnum>&&znum>)
{
if(nowstep>){ nowstep--; Z_hp-=totpnum; } //没走到豌豆射手处
else{ P_hp-=znum; Z_hp-=totpnum; } //走到了两个都要受伤害
if(P_hp<=) //死一个射手
{
P_hp=;
totpnum--; pnum--;
nowstep=;
}
if(Z_hp<=){ Z_hp=; znum--; } //死一个僵尸
}
if(pnum<=&&znum>) return true; //所有豌豆射手挂掉而僵尸有多的
return false;
}
int BIS(int totpnum,int pnum) //二分
{
int x=,y=*maxn,mid;
while(x<=y)
{
mid=(x+y)/;
if(judge(totpnum,pnum,mid)) y=mid-;
else x=mid+;
}
return y+;
}
int solve()
{
int ret=; //ret是需要的僵尸的个数
if(S[L]=='M') { ret++; L--; step=; } //最右边是土豆,step代表走到下一个位置需要的步数
else step=;
while(L>=) //知道小于0才算赢
{
if(S[L]=='M') //是土豆
{
if(step>) //往前走
{
step--;
if(Pnum>=) ret++; //超过50个豌豆射手死一个僵尸
continue;
}
else ret++; //炸死一个
Mnum--; L--;
step=; //step变为2
}
else //如果是豌豆射手
{
int pnum=;
for(int i=L;i>=;i--) //一直到找到土豆
if(S[i]=='M') break;
else pnum++;
ret+=BIS(Pnum,pnum); //二分得到最小的僵尸个数
Pnum-=pnum;
L-=pnum+; //多减1表示把土豆算进去了,因为有没死的僵尸在土豆处牺牲
step=;
}
}
if(S[]=='M') ret++; //如果最左边是土豆,则还需要一个僵尸去攻击首脑
return ret;
}
int main()
{
int T,Case=;
scanf("%d",&T);
while(T--)
{
scanf("%s",S); //植物
init();
printf("Case %d: %d\n",++Case,solve());
}
return ;
}
Hdu3640-I, zombie(模拟+二分)的更多相关文章
- FZU 1575 小学生的游戏【模拟二分】
某天,无聊的小斌叫上几个同学玩游戏,其中有比较笨的小兴,比较傻的小雪,可爱的小霞和自以为是的小楠.他们去找聪明的小明去给他们当裁判.判定谁取得游戏胜利. 而这个游戏是由小斌想个1到10000000的数 ...
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A B C D 水 模拟 二分 贪心
A. Is it rated? time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- UVALive 3971 Assemble(模拟 + 二分)
UVALive 3971 题意:有b块钱.想要组装一台电脑,给出n个配件的种类,名字,价格,品质因子.若各种类配件各买一个,总价格<=b,求最差品质配件的最大品质因子. 思路: 求最大的最小值一 ...
- bzoj4717 改装 模拟+二分
Description [题目背景] 小Q最近喜欢上了一款游戏,名为<舰队connection>,在游戏中,小Q指挥强大的舰队南征北战,从而成为了一名dalao.在游戏中,不仅船只能力很重 ...
- Largest Allowed Area【模拟+二分】
Largest Allowed Area 题目链接(点击) 题目描述 A company is looking for land to build its headquarters. It has a ...
- Codeforces Round #481 (Div. 3) C. Letters (模拟,二分)
题意:有个\(n\)个公寓,每个公寓\(a_{i}\)代表着编号为\(1-a_{i}\)个房间,给你房间号,问它在第几栋公寓的第几个房间. 题解:对每个公寓的房间号记一个前缀和,二分查找属于第几个公寓 ...
- 模拟+二分 poj-1019-Number Sequence
题目链接: http://poj.org/problem?id=1019 题目大意: Sk表示123...k 把S1S2S3...Sk排成一行 比如:112123123412345123456.... ...
- Codeforces 404E: Maze 1D(二分)
题意:指令“R”机器人会向右走一步,“L”是向左.起初机器人在0位置,可以在除了0以外的任何位置放障碍,如果机器人的指令将使它走到障碍上,那这一步他会保持不动.要求让机器人最终结束的那一步一定只走过一 ...
- NOIP模拟测试7
期望得分:60+60+60 实际得分:60+60+0 这次考试主要是T3搜索打挂了(我可是靠搜索吃饭的); 1.数组开小了,不过开大数组只拿到了10分的好成绩. 2.题意没审清(其实是他没说清). 以 ...
随机推荐
- 【剑指offer】面试题40:数组中只出现一次的数字
题目: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 思路: 因为只有两个只出现一次的数字,所以所有数字进行异或之后得到值res一定不是0.这样,res ...
- python手记(39)
#!/usr/bin/env python #-*- coding: utf-8 -*- #code:myhaspl@qq.com import cv2 import numpy as np fn=& ...
- WPF多线程问题
最近碰到这种多线程问题都是在WPF项目中. 1. 问题是这样.有个一主界面线程,然后background线程启动,这个background线程试图去修改主界面里面的数据. 造成死锁. 调用过程,主界面 ...
- java servlet+jquery+json学习小例子
引入JSON的jar包: 注意,如果包不全,页面请求servlet时,jquery ajax会返回error:function 弹出error occured!!! HTML Code: <%@ ...
- 解决selenium 启动ie浏览器报错:Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones
启动ie代码: System.setProperty("webdriver.ie.driver", "bin/IEDriverServer.exe"); Web ...
- 获取java类和方法名
String clazz = this.getClass().getName(); String method = Thread.currentThread() .getStackTrace()[1] ...
- python3-day3(函数-返回值)
1.函数 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 2.return返回值 import smtplibfro ...
- ZigBee心电传输(一)
第一次接触模拟的东西哈,也算是一次新的学习旅程以及对ZigBee的再一次探索吧. 首先是方案制定,以及采用芯片AD8232,这样节省了不少时间,把模拟的东西都搬到数字上了,不过还是需要学习不少模电知识 ...
- hdu 4666 Hyperspace
曼哈顿距离,两个点设为(x1,y1),(x2,y2),其距离为|x1-x2|+|y1-y2| #include <cstdio> #include <set> #include ...
- 选择Android还是IOS开发?
选择Android还是IOS? 随着移动互联网的如日中天,涌现了越来越多的开发者.IOS优秀的用户体验,Android极高的用户群,这对于开发者来说陷入了选择困难的境地,尤其是新入门的开发者,精力有限 ...