ZOJ - 4062 Plants vs. Zombies (二分)(2018ICPC青岛E)

版权声明:Why is everything so heavy? https://blog.csdn.net/lzc504603913/article/details/83824416

Plants vs. Zombies


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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


Plants vs. Zombies(?)
(Image from pixiv. ID: 21790160; Artist: socha)

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.

题意:机器人走过一个花,可以给那个花浇水,给定步数下,问花的最小的最大能量值。

解题思路:求最大的最小必然要二分。我们直接二分答案,然后从前往后暴力的计算每个点满足答案要多少次,我们跟它右边那个反复横跳即可。然后记录需要使用跳多少次即可。最后只需判断次数是否大于M即可。但是这题很多坑,详见代码。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 100005;

int N;
ll M;
ll a[MAXN];
ll b[MAXN];

bool judge(ll m){
    if(m==0)
        return true;
    for(int i=1;i<=N+1;i++)
        b[i]=0;
    for(int i=1;i<=N;i++){
        ll num=(m+a[i]-1)/a[i];
        //这句不能省,倒数第二个跟最后一个反复横跳时,可能已经把最后一个满足了,这样可以省一步
        if(i==N&&b[i]>=num)
            break;
        b[i]++;
        if(b[i]<num){
            b[i+1]+=num-b[i];
            b[i]=num;
        }
    }
    ll tmp=0;
    for(int i=1;i<=N+1;i++){
        tmp+=b[i];
        if(tmp>M)//必须放里面判断,否则会爆ll
            return false;
    }
    return true;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%lld",&N,&M);
        for(int i=1;i<=N;i++)
            scanf("%lld",&a[i]);
        ll l=0,r=1e18;
        while(l<r){
            ll m=(l+r)/2;
            if(judge(m))
                l=m+1;
            else
                r=m;
        }
        printf("%lld\n",l-1);

    }

    return 0;
}


猜你喜欢

转载自blog.csdn.net/lzc504603913/article/details/83824416
今日推荐