Plants vs. Zombies(2018ICPC青岛站E题) 二分

怎甘沉沦 提交于 2020-05-04 06:13:32

Plants vs. Zombies

                                                ZOJ - 4062 

BaoBao and DreamGrid are playing the game Plants vs. Zombies. In the game, DreamGrid grows plants to defend his garden against BaoBao's zombies

There are  plants in DreamGrid's garden arranged in a line. From west to east, the plants are numbered from 1 to  and the -th plant lies  meters to the east of DreamGrid's house. The -th plant has a defense value of  and a growth speed of . Initially,  for all .

DreamGrid uses a robot to water the plants. The robot is in his house initially. In one step of watering, DreamGrid will choose a direction (east or west) and the robot moves exactly 1 meter along the direction. After moving, if the -th plant is at the robot's position, the robot will water the plant and  will be added to . Because the water in the robot is limited, at most  steps can be done.

The defense value of the garden is defined as . DreamGrid needs your help to maximize the garden's defense value and win the game.

Please note that:

  • Each time the robot MUST move before watering a plant;
  • It's OK for the robot to move more than  meters to the east away from the house, or move back into the house, or even move to the west of the house.
Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains two integers  and  (), indicating the number of plants and the maximum number of steps the robot can take.

The second line contains  integers  (), where  indicates the growth speed of the -th plant.

It's guaranteed that the sum of  in all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the maximum defense value of the garden DreamGrid can get.

Sample Input
2
4 8
3 2 6 6
3 9
10 10 1
Sample Output
6
4
Hint

In the explanation below, 'E' indicates that the robot moves exactly 1 meter to the east from his current position, and 'W' indicates that the robot moves exactly 1 meter to the west from his current position.

For the first test case, a candidate direction sequence is {E, E, W, E, E, W, E, E}, so that we have  after the watering.

For the second test case, a candidate direction sequence is {E, E, E, E, W, E, W, E, W}, so that we have  after the watering.

题意:输入第一行为样例数,每个样例的第一行为n,m,代表有n个数字和要走m步,第二行为n个数字的价值,每走到一个数字,该数字所在位置就要加上这个位置的价值。问走完m步之后最小值的最大值为多少。

思路:二分

    l=0,r=1e12 二分可以的最小价值的最大值。以第一个样例为准,a[1]=3 a[2]=2 a[3]=6 a[4]=6 如果最小值为7,那么位置1就需要走3次,即走到2,走回1,走到2,走回1。所以当1位置被走了3次后,2位置其实已经被走过了2次。以此类推,用step记录走的步数,先算出在某个二分的值下每个位置最少需要走几步,然后遍历一遍,看看真实走的步数是否比m小,如果小,说明当前二分的值偏小,否则偏大。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 typedef long long ll;
 7 const int maxn=1e5+10;
 8 int casen;
 9 int n;
10 ll m;
11 ll a[maxn],b[maxn];
12 bool check(ll x)
13 {
14     ll step=0;
15     for(int i=0;i<n;i++)//先计算一遍在以x为最小值的情况下,每一个位置应该走多少步 
16     {
17         if(x%a[i])
18         {
19             b[i]=x/a[i]+1;
20         }
21         else
22         {
23             b[i]=x/a[i];
24         }
25     }
26     //(for(int i=0;i<n;i++) b[i]=(x+a[i]-1)/a[i];) 大佬是这么写的,真简洁啊 
27     for(int i=0;i<n-1;i++)
28     {
29         step++;        //,每经过一个位置,加一步 
30         b[i]--;        
31         if(b[i]<=0)
32             continue;
33         step+=2*b[i];//因为每次想让当前位置再减小的话,必定是向前然后回来,一来一回需要两倍的步数 
34         b[i+1]-=b[i];//将把因为b[i]减到0而走过的步数减去 
35         if(step>m)
36             return false;
37     }
38     if(b[n-1]>0)
39     {
40         step++;         
41         b[n-1]--;
42         step+=b[n-1]*2;
43     }
44     if(step>m)
45         return false;
46     return true;
47 }
48 int main()
49 {
50     scanf("%d",&casen);
51     while(casen--)
52     {
53         scanf("%d%lld",&n,&m);//注意m的范围!!!! 
54         for(int i=0;i<n;i++)
55         {
56             scanf("%lld",&a[i]);
57         }
58         ll l=0,r=1e12+10;
59         ll ans=0;            //注意写=0啊啊啊啊啊!!!!! 
60         while(l<=r)
61         {
62             ll mid=(l+r)/2;
63             if(check(mid))
64             {
65                 ans=mid;
66                 l=mid+1;
67             }
68             else
69             {
70                 r=mid-1;
71             }
72         }
73         printf("%lld\n",ans);
74     }
75 }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!