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. json最大长度限制_GET请求中URL的最大长度限制总结,读完之后,大部分程序员收藏了...
  2. GEO数据下载及处理详细过程
  3. php 使用redis
  4. HYSBZ/BZOJ 1038 [ZJOI2008] 瞭望塔 - 计算几何
  5. JS中变量和函数的使用
  6. Windows 8.1 新增控件之 Hyperlink
  7. 基于前端javascript的搜索功能
  8. 快手小剧场推出独立APP“追鸭”
  9. Node.js下载安装及各种npm、cnpm、nvm、nrm配置(保姆式教程—提供全套安装包)—cnpm的安装与配置(3)
  10. 安卓反编译揭秘,伪加密APK文件如何被破坏
  11. python重复字符串n次的函数_LeetCode_Python(3)_无重复字符的最长子串
  12. 求素数--筛选法和打表
  13. Xcode7下模拟器输入文本无法显示系统键盘的解决办法
  14. 什么是java OOM Out Of Memory 内存溢出?如何分析及解决oom问题?
  15. 企业开展自媒体推广,重点是什么?
  16. 玫瑰花绘制python_Python玫瑰花绘制-Go语言中文社区
  17. ubuntu系统下的文本编辑器
  18. 浏览器无法访问hdfs界面问题
  19. 实验三+070+胡阳洋
  20. java蓝桥杯数字黑洞_【蓝桥杯】数字黑洞(5位黑洞数)

热门文章

  1. USRP系列(四):USRP RIO 和 Stand-alone USRP
  2. 03 计算机网络-网络层和数据链路层专题
  3. java计算机毕业设计教务管理系统源码+数据库+系统+lw文档+mybatis+运行部署
  4. 物联网充电桩(电动自行车)管理方案
  5. NOI题库 数论 相关的题目 汇总-2022.01.22
  6. Elastic实战:canal自定义客户端,实现mysql多表同步到es
  7. 视频剪辑工具,教你批量分割视频并提取原音频单独保存
  8. 配置coredns解析公网域名
  9. Linux下deb安装包的安装教程
  10. Windows 下启动tomcat