【HDU 3640】I, zombie (二分)

传送门

题目描述

The "endless" model in "I, zombie" of "Plants vs. Zombies" is my favourite.
The aim of the game is to put down the zombies most reasonable in the right-most , to eat the brain protected by Plants in the left-most.


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?

题目翻译

现在有一排植物,你可以每一秒从最右边的位置放一个僵尸。

每一秒:

1)在右侧可以放一个或多个僵尸。

2)如果一个僵尸在豌豆射手上,那么那个豌豆射手-1HP(就算是负的还算活),否则向前走一步。

3)如果地图上有僵尸,那么最先放上去的僵尸-1HP(但是还活着)。

4)如果有僵尸踩在土豆雷上,那么这一格上的僵尸HP=0。

扫描二维码关注公众号,回复: 1550808 查看本文章

5)清除掉没血的植物和僵尸。

现在问你至少需要多少只僵尸来吃掉所有的大脑。

解题思路

我们从贪心的角度入手,一旦遇到土豆雷,最好是只有一个僵尸踩上去,不然会死好多僵尸。并且我们一定要利用到前面的僵尸来挡子弹,每次遇到土豆雷的最好情况是只有一个被炸死,但是这只僵尸身后可以有很多僵尸,这样就不需要每次都从右边走到左边来浪费血了。

我们再来分析具体情况,如果一个僵尸被土豆雷炸死了,那么他后面的僵尸应该要走两步才能到达前面的植物,我们不妨分段处理,每次遇到土豆雷停下,然后处理连着的一段豌豆射手,这样会好分析一些。

如果有一排连续的豌豆射手,这个时候怎么算呢?  二分就可以(但是蒟蒻觉得应该可以直接推出公式qwq)。

注意每次僵尸到植物的距离,如果僵尸还有一格到达豌豆,那么如果总植物的个数比50大的话,就要牺牲一只僵尸。

注意处理一下最前面和最后面的土豆雷,最后的雷会炸死一只,但是还要再放一只,才能吃到脑子。

 

注意点 

如果僵尸离最近的豌豆距离为1,那么要判断植物的总量,因为植物数量大于等于50的时候,就会打死一只僵尸。

但是如果前面的是一个土豆雷,管他有几个豌豆射手,这个僵尸一定被炸死!!!!

如果前面两个格子是一个土豆雷,那么才有可能被射死。

还要注意二分的时候要留一只僵尸来踩前面的土豆雷,或者去吃脑子!!

代码

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 int T,len,step,P,ans;
 5 char ch[200];
 6 inline int check(int tot_plant,int int_plant,int z){
 7     int step1=step,plant=10,zombie=50;
 8     while(int_plant>0&&z>0){
 9         if(step1)zombie-=tot_plant,step1--;
10         else plant-=z,zombie-=tot_plant;
11         if(zombie<=0)zombie=50,z--;
12         if(plant<=0)plant=10,tot_plant--,int_plant--,step1=1;
13     }
14     if(int_plant<=0&&z>0)return 1;
15     return 0;
16 }
17 int main(){
18     cin>>T;
19     for(register int t=1;t<=T;t++){
20         scanf("%s",ch);
21         len=strlen(ch)-1;
22         ans=P=step=0;
23         for(register int i=0;i<=len;i++)
24             if(ch[i]=='P')P++;
25         if(ch[len]=='M')ans++,len--,step=2;
26         else step=1;
27         while(len>=0){
28             if(ch[len]=='M'){
29                 if(step==2){
30                     step--;
31                     if(P>=50)ans++;
32                     continue;
33                 }
34                 ans++,step=2,len--;
35             }
36             else {
37                 int int_plant=0;
38                 for(register int i=len;i>=0&&ch[i]=='P';i--)int_plant++;
39                 register int l=0,r=400,m;
40                 while(l<r){
41                     m=(l+r)>>1;
42                     if(check(P,int_plant,m))r=m;
43                     else l=m+1;
44                 }
45                 ans+=l,P-=int_plant,len=len-(int_plant+1),step=2;
46             }
47         }
48         if(ch[0]=='M')ans++;
49         printf("Case %d: %d\n",t,ans);
50     }
51 }

猜你喜欢

转载自www.cnblogs.com/Fang-Hao/p/9164214.html