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;
}

ZOJ 4062 Plants vs. Zombies(二分)相关推荐

  1. ZOJ 4062 Plants vs. Zombies(二分答案)

    题目链接:Plants vs. Zombies 题意:从1到n每个位置一棵植物,植物每浇水一次,增加ai高度.人的初始位置为0,人每次能往左或往右走一步,走到哪个位置就浇水一次.求m步走完后最低高度的 ...

  2. ZOJ 4062 Plants vs. Zombies 2018 ICPC 青岛站 E Plants vs. Zombies

    ZOJ 4062 二分 哎,二分,二分,二分,我咋就没想到啊,在一篇博客上看到一句话: 一般此类最小值最大问题都是二分,此题显然也是可以二分植物的高度的. 博客链接:https://www.cnblo ...

  3. 2018青岛ICPC ZOJ 4062: Plants vs. Zombies(二分)

    题意: 一条横轴上有n棵植物,第i棵植物在位置i上,生长速度为di,初始高度都为0,你的家在位置0上 你有一个洒水车,第0秒时在家门口(位置0),之后它每1秒都可以往左或往右移动一个单位(往左往右自己 ...

  4. zoj4062 Plants vs. Zombies 二分+模拟(贪心的思维)

    题目传送门 题目大意:有n个植物排成一排,标号为1-n,每株植物有自己的生长速度ai,每对植物浇一次水,该株植物就长高ai,现在机器人从第0个格子出发,每次走一步,不能停留,每一步浇一次水,总共可以走 ...

  5. ZOJ4062 Plants vs. Zombies 二分

    ZOJ4062 Plants vs. Zombies 一机器人给植物浇水,浇水之前必须移动一格,移动后必须浇水,从1~n的位置上有n个植物,每个植物都有自己的生长速度, 每浇一次生长一次,机器人初始位 ...

  6. 2018 ACM-ICPC 亚洲区域赛青岛站 E - Plants vs. Zombies(二分)

    题意 有n个植物,m次移动1格的机会, 以下n个数a1-an,代表每一次浇水(其实就是访问),该处的植物会增加防御值ai,初始防御值di=0 n个植物分别在坐标轴1,-,n的位置,浇水机的位置初始在0 ...

  7. Plants vs. Zombies【二分】

    ZOJ - 4062 Plants vs. Zombies BaoBao and DreamGrid are playing the game Plants vs. Zombies. In the g ...

  8. Plants vs. Zombies(二分)

    文章目录 [Plants vs. Zombies](https://zoj.pintia.cn/problem-sets/91827364500/problems/91827370312) 题意 解题 ...

  9. Plants vs. Zombies

    ZOJ - 4062 BaoBao and DreamGrid are playing the game Plants vs. Zombies. In the game, DreamGrid grow ...

最新文章

  1. 状态栏消息提示——使用Notification
  2. json字符串转换成json对象,json对象转换成字符串,值转换成字符串,字符串转成值...
  3. linux查看进程相关命令
  4. comsol稀物质传递_印刷指南丨印刷油墨传递的影响因素?
  5. c语言中图形驱动程序功能_C / C ++中的图形:一些更有趣的功能
  6. POJ 1417 True Liars 并查集+背包
  7. 又开火了!马斯克炮轰贝佐斯:建议分拆亚马逊!
  8. 关于继承中调用成员变量和局部变量以及如何区分子类和父类中的变量,白话文详解,适合刚刚接触编程的新手
  9. 闫啸的发明和发现20220901
  10. 十字链表实现矩阵存储
  11. cad2019菜单栏怎么调出来_cad怎样调出菜单栏(cad2016工具栏怎么调出来)
  12. dota2服务器切换账号,DOTA2网络卡顿得到改善 全面更换电信服务器机房
  13. 用python解决题目:输入某年某月某日,判断这一天是这一年的第几天?
  14. 车市冷车道 这6款紧凑车苦坐板凳寻伯乐(一)
  15. 共享充电线项目市场分析报告
  16. 区块链金融中的python应用--LSM定价
  17. 群翔ShopNum1分销系统V8.1升级版,更优更全更盈利
  18. 读地质图总结(瞎写的)
  19. 抖音私信引流话术,抖音私信引流技术
  20. c/c++ register函数

热门文章

  1. 微信小程序账号长时间未登录冻结解封
  2. TRS过期后解决办法
  3. 华纳云:盘点那些年操作系统的成长史
  4. CPU性能是否受年龄影响?
  5. Car-eye 智能车辆管理云平台以报警为中心展开业务
  6. 无线系统笔记(2)----麦克斯韦方程组
  7. 【python】——Python中的*和**的作用和含义
  8. 三、Git本地仓库基本操作——git仓库忽略跟踪文件
  9. [整理]程序员一生必读的30本书
  10. 程序员可以时光倒流的话,你还会选择IT行业吗?再累也愿意!