Hdu3640-I, zombie(模拟+二分)

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.题意没审清(其实是他没说清). 以 ...
随机推荐
- ZOJ3519-Beautiful People:最长上升子序列的变形
Beautiful People Special JudgeTime Limit: 10000/5000MS (Java/Others)Memory Limit: 128000/64000KB (Ja ...
- Spark Accumulators
概述 Accumulator即累加器,与Mapreduce counter的应用场景差不多,都能很好地观察task在运行期间的数据变化,Spark中的Accumulator各task可以对Accumu ...
- Android 通知栏用法例子
当程序意外退出时,可以去掉通知栏上显示的图标 1.创建TestNotificationActivity activity类, package com.notioni.test.notification ...
- Oracle查找重复数据
Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)
- mysql中常用的语句整理
mysql中常用的语句: 1:创建带自增长的主键的表 DROP TABLE IF EXISTS user_login ; CREATE TABLE user_login ( user_id INT ...
- 数据库对象(视图,序列,索引,同义词)【weber出品必属精品】
视图视图的定义:视图就是一个查询的别名为什么使用视图限制数据的存取 SQL> conn /as sysdba 已连接. SQL> grant create view to scott; 授 ...
- 空值排序(oracle和sqlserver)
oracle认为 null 最大. 升序排列,默认情况下,null值排后面. 降序排序,默认情况下,null值排前面. 改变空值办法: (1)用nvl函数或decode函数将null转换为一特定值 替 ...
- JS控制文本框textarea输入字数限制的方法
<html> <head runat="server"> <title></title> <script type=" ...
- zeromq源码分析笔记之无锁队列ypipe_t(3)
在上一篇中说到了mailbox_t的底层实际上使用了管道ypipe_t来存储命令.而ypipe_t实质上是一个无锁队列,其底层使用了yqueue_t队列,ypipe_t是对yueue_t的再包装,所以 ...
- Qt Creator的配置
说明:一直想入手QT,看了相关的教程也有一段时间了,但苦于安装QT编辑器一直没有成功,今天手痒痒,于是又来捣鼓一阵子,成功了,特记录下来,方便日后查阅: 环境:win7 x64 + QT Creat ...