ZOJ 4062 Plants vs. Zombies(二分)

ZOJ 4062 Plants vs. Zombies

在这里插入图片描述

意义不明的花妈和芳香。

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<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
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]<mid)
        {
            long long s=((mid-tmp[i]-1)/a[i])+1;//当前位置需要再浇几遍花
        //新姿势,对于5/3,(5-1)/3+1==2。
        //        对于3/3,(3-1)/3+1==1。
            now=now+s*2;
            tmp[i+1]=tmp[i+1]+s*a[i+1];//在两个位置之间移动会更新后面位置的值
        }
    }
    if(now>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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值