题目传送门

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

思路:

  一般此类最小值最大问题都是二分,此题显然也是可以二分植物的高度的。

  确定某一个高度后,也确定了每个植物需要浇几次水,而对于一株植物来说,应当尽可能的在这株植物和后面那个格子来回走,是这株植物迅速超过最低高度(这样的走法是最优的,因为可以想象,如果往后走很多步再走回来,中间浪费的可能性比较大),于是就是对n个植物模拟浇水,考虑一些细节就可以了(二分跳出条件,long long等等)

//#pragma comment(linker,"/STACK:102400000,102400000")
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<stdlib.h>
//#include<unordered_map>
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)|1
#define CLR(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
typedef long long ll;
using namespace std;
inline ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
const int maxn=100010;
int n;
ll a[maxn],c[maxn],m;
inline bool judge(ll high){CLR(c,0);ll temp=m;if(m==0)return false;c[1]=a[1],m--;int i=1;for(;i<=n;i++){if(m<=0)break;if(c[i]>=high){if(m>0){c[i+1]=a[i+1];m--;continue;}else{break;}}ll tmp=(ll)ceil((high-c[i])*1.0/a[i]);if(m>2*tmp){m-=2*tmp+1;c[i]+=a[i]*tmp;c[i+1]+=a[i+1]*(tmp+1);continue;}else if(m==2*tmp){c[i]+=a[i]*tmp;c[i+1]+=a[i+1]*tmp;break;}else{break;}}m=temp;for(i=1;i<=n;i++){if(c[i]<high)return false;}return true;
}
int main(){int t;cin>>t;while(t--){cin>>n>>m;ll l=0,r=0,mid,ans;for(int i=1;i<=n;i++){a[i]=read();r=max(r,a[i]*m);}if(m==0){printf("0\n");continue;}while(l<=r){mid=(l+r)>>1;//    printf("mid  %d\n",mid);if(judge(mid)){ans=mid;l=mid+1;}else{r=mid-1;}}printf("%lld\n",ans);}
} 

View Code

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.

转载于:https://www.cnblogs.com/mountaink/p/9921988.html

zoj4062 Plants vs. Zombies 二分+模拟(贪心的思维)相关推荐

  1. ZOJ4062 Plants vs. Zombies 二分

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

  2. 2018ICPC青岛区域赛 zoj4062 Plants vs. Zombies

    2018ICPC青岛区域赛 zoj4062 Plants vs. Zombies BaoBao and DreamGrid are playing the game Plants vs. Zombie ...

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

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

  4. 【CodeForces - 985D】Sand Fortress (二分,贪心,思维构造,技巧,有坑)

    题干: You are going to the beach with the idea to build the greatest sand castle ever in your head! Th ...

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

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

  6. ZOJ 4062 Plants vs. Zombies(二分)

    ZOJ 4062 Plants vs. Zombies 意义不明的花妈和芳香. There are plants in DreamGrid's garden arranged in a line. F ...

  7. Plants vs. Zombies(二分)

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

  8. Plants vs. Zombies【二分】

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

  9. Plants vs. Zombies

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

最新文章

  1. 智慧工地-基于深度学习yolov3的安全帽检测以及安全帽检测数据集(1)
  2. Java中图形界面重绘方法
  3. keyvaluepair_C# KeyValuePairTKey,TValue的用法【转】 .
  4. 背包问题九讲 v1.0
  5. 三星1TB硬盘MM804RS不识别加电磁头打盘敲盘异响不识别开盘恢复数据
  6. JavaScript快速入门-ECMAScript本地对象(String)
  7. 华为云计算机访问手机软件,华为云电脑来了,只需一个APP就能让手机秒变Windows电脑!...
  8. 使用 telnet 命令 查看端口的开放、可用情况
  9. 与或非运算(布尔值/非布尔值)
  10. 调查:Win7是勒索病毒的重灾区 XP受影响不足0.1%
  11. 【java与移动设备】CH06 资源的使用
  12. Ubuntu菜鸟入门(五)—— 一些编程相关工具
  13. android编译log中_安卓编译 Jack server 错误问题解决办法
  14. plsql导入导出表结构和数据对象
  15. 自己处理域名、主机备案流程 + 公网安备流程,总共耗时半个月左右
  16. 微信自定义分享功能;分享朋友,分享到朋友圈
  17. GPS公司内部管理系统
  18. Top 7大开源数据可视化分析工具!
  19. 解锁的Facebook其实是很容易的
  20. redis实现高并发投票网站

热门文章

  1. java基础学习——5、HashMap实现原理
  2. webstorm环境安装配置(less+autoprefixer)
  3. 16.U-boot的工作流程分析-2440
  4. (百度、谷歌)地图经纬度gps偏移解决办法:gps纠偏数据库纠偏
  5. 网速正常,一打开IE总是卡很久
  6. 实例演示oracle注入获取cmdshell的全过程
  7. 安装openssl-devel命令
  8. Alpha冲刺第二天
  9. SQLServer之函数简介 1
  10. 使用ActionTrail Python SDK