ZOJ 4062 Plants vs. Zombies(二分)

ZOJ 4062 Plants vs. Zombies

ZOJ 4062 Plants vs. Zombies(二分)_第1张图片

意义不明的花妈和芳香。

There are plants in DreamGrid’s garden arranged in a line. From west to east, the plants are numbered from 1 to n 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 ai . Initially, di=0,for all 1<=i<=n.

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 ai will be added to di. Because the water in the robot is limited, at most m steps can be done.

The defense value of the garden is defined as min{di|1<=i<=n}. 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 n (2<=n<=10^5) and m (0<=m<=10^12 ), indicating the number of plants and the maximum number of steps the robot can take.

The second line contains n integers a1,a2,…,an (1<=ai<=10^5), where indicates the growth speed of the -th plant.

It’s guaranteed that the sum of in all test cases will not exceed 10^6.

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

题意:

你有一些花和一个机器人,机器人每次可以向左或向右移动一步来浇一次花,每次浇花都会使被浇的花茁壮值增加,问你在规定的步数内茁壮值最小的花茁壮值 最大可以为多少(默认机器人一开始在第一盆花的左边,所有花的初始茁壮值为0)。
T组输入。每组输入有两个数n和m,表示一共有n盆花,机器人可以移动m步。接下来有n个数字,每个数字表示第i盆花被浇之后会增加的茁壮值。每组输出一个数,表示茁壮值最小的花最大值为多少。

题解:

首先看到最小值最大可以想到题目可以用二分答案来做。再根据贪心思想,因为机器人每移动一次才会浇一次花,那么对于需要浇好多次的花肯定需要走过去后再回来浇,所以得到的情况就是机器人在这盆花和下一盆花之间来回移动。
我们不断二分一个答案,然后计算对于使每盆花满足答案的机器人移动步数之和是否在m步之内,最后得到满足条件的最大值。

注意:在运算过程中可能会爆掉long long,对于这种情况直接返回0就可以。

#include
#include
#include
#include
using namespace std;
long long a[200000+10];
long long tmp[200000+10];
int n;
long long m;
long long find(long long mid)
{
    for(int i=1;i<=n;i++)
    {
        tmp[i]=0;
    }
    long long now=0;
    for(int i=1;i<=n;i++)
    {
        if(i==n)//特判,对于第n处,如果之前处理n-1处时已经使第n处符合条件,则后面不需要再走
        {
            if(tmp[i]>=mid)
            {
                break;
            }
        }
        now++;
        tmp[i]+=a[i];
        if(tmp[i]m||now<0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}
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]);
        }
        long long l=0,r=1e18;
        long long ans=0;
        while(l<=r)
        {
            long long mid=(l+r)/2;
            if(find(mid))
            {
                l=mid+1;
                ans=max(ans,mid);
            }
            else
            {
                r=mid-1;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

你可能感兴趣的:(东方,二分)