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.


Author: WANG, Yucheng; CHEN, Shihan

Source: The
2018 ACM-ICPC Asia Qingdao Regional Contest


【题意】

从左到右依次是1个房子+n个植物,从房子出发给n个植物浇水,每一次可以往左可以往右,起始时植物的防御值都为0,每一次在i位置浇一次水,防御增加d[i],每一次必须走,不能呆在原地,可以走出去,定义n个植物的防御力为min(ai),1<=i<=n,问怎样走使得这些植物防御力最小值最大


【分析】

l=0,r=1e12
二分可以的最小价值的最大值。以第一个样例为准,a[1]=3 a[2]=2 a[3]=6 a[4]=6
如果最小值为7,那么位置1就需要走3次,即走到2,走回1,走到2,走回1。所以当1位置被走了3次后,2位置其实已经被走过了2次。以此类推,用step记录走的步数,先算出在某个二分的值下每个位置最少需要走几步,然后遍历一遍,看看真实走的步数是否比m小,如果小,说明当前二分的值偏小,否则偏大

 


【代码】

#include<cstdio>
using namespace std;
typedef long long ll;
inline ll read(){
register char ch=getchar();register ll x=0;
for(;ch<'0'||ch>'9';ch=getchar());
for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x;
}
const int N=1e5+5;
int T,n;ll m,a[N],c[N],d[N];
inline bool check(ll now){
if(!now) return 1;
ll tot=m;
for(int i=1;i<=n;i++) c[i]=(now-1)/a[i]+1,d[i]=0;
//简洁化if(now%a[i]) c[i]=now/a[i]+1;else c[i]=now/a[i];
for(int i=1;i<=n;i++){
if(i==n&&c[i]<=d[i]) return 1;
if(tot<=0) return 0;
d[i]++,tot--;
if(c[i]<=d[i]) continue;
ll need=c[i]-d[i];
if((need<<1)>tot) return 0;
tot-=need<<1;//因为想让当前位置满足次数的话,必定是一来一回,需要两倍的步数
d[i+1]+=need;//d[i]对d[i+1]的贡献
}
return 1;
}
int main(){
for(T=read();T--;){
n=read();m=read();
for(int i=1;i<=n;i++) a[i]=read();
ll l=0,r=1e12,ans=0;
while(l<=r){
ll mid=l+r>>1;
if(check(mid)){
ans=mid;
l=mid+1;
}
else{
r=mid-1;
}
}
printf("%lld\n",ans);
}
return 0;
}

2018 青岛ICPC区域赛E ZOJ 4062 Plants vs. Zombie(二分答案)的更多相关文章

  1. ZOJ 4062 - Plants vs. Zombies - [二分+贪心][2018 ACM-ICPC Asia Qingdao Regional Problem E]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4062 题意: 现在在一条 $x$ 轴上玩植物大战僵尸,有 $n$ ...

  2. 2018 ACM-ICPC亚洲区域赛(青岛)

    Problem C---zoj 4060 Flippy Sequence 解题思路:要求进行两次操作,每次操作选择一个区间,问将s串变成t串中所选的两个区间构成的4元组有多少个.做法:找出s串与t串不 ...

  3. UVALive 7146 (贪心+少许数据结构基础)2014acm/icpc区域赛上海站

    这是2014年上海区域赛的一道水题.请原谅我现在才发出来,因为我是在太懒了.当然,主要原因是我刚刚做出来. 其实去年我就已经看到这道题了,因为我参加的就是那一场.但是当时我们爆零,伤心的我就再也没有看 ...

  4. 2018 ICPC 区域赛 焦作场 D. Keiichi Tsuchiya the Drift King(计算几何)

    http://codeforces.com/gym/102028/problem/D 题意:根据题中给的那个图,然后题目给你 a,b,r,d,让你求出最小的满足矩形通过弯道的w 思路:

  5. 2018 ACM-ICPC 亚洲区域赛青岛现场赛 —— Problem F. Tournament

    题面:http://acm.zju.edu.cn/contest-materials/qd2018/qd2018_problems.pdf 题意: n个骑士决斗K轮 要求是每个骑士只能跟另外一个骑士决 ...

  6. ACM-ICPC 2018 青岛赛区现场赛 K. Airdrop && ZOJ 4068 (暴力)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4068 题意:吃鸡游戏简化为二维平面上有 n 个人 (xi,yi) ...

  7. ACM-ICPC 2018 青岛赛区现场赛 D. Magic Multiplication && ZOJ 4061 (思维+构造)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4061 题意:定义一个长度为 n 的序列 a1,a2,..,an ...

  8. 2018南京icpc现场赛心得

    第一次参加icpc的比赛,也是第一块奖牌,虽然只是铜,但其实打的已经很好了,稍微差一点就可以摸银了. 之前参加省赛,成为那次比赛我校唯一一个没拿奖的队伍,其实还是一直都有一些心结的,而这段时间和新的队 ...

  9. 2018 焦作icpc现场赛总结

    Day 0 没有直达焦作的飞机,所以选择了先到新郑机场,再转乘城际列车.城际列车猜是专门给学生开通的吧,每天只有来和回一共两趟(所以机票选择的余地也不多).买的时候只有无座票了,本来以为会一直站着,但 ...

随机推荐

  1. 'AndroidManifest.xml' must have a minimum of 2 segments.

    Manifest下必须有二级域名 <manifest xmlns:android="http://schemas.android.com/apk/res/android" p ...

  2. 用shell 实现对MySQL数据库分页

    参考链接 http://mp.weixin.qq.com/s?__biz=MzAxMzE4MDI0NQ==&mid=208299533&idx=1&sn=4cab00793eb ...

  3. jquery datepicker只显示年和月

    <html xmlns="http://www.w3.org/1999/xhtml"> <head >     <title></titl ...

  4. XML 中可嵌入 cmd命令脚本

    原文要参照代码 1. XML解析 Task逻辑块可相互组合,形成复杂的树状结构,其结构用XML表示,即写成XML文件的形式. 样例如下: <!-- 顺序执行块 --> <seq> ...

  5. 通过expect免自动输入密码登陆远程服务器

    通过expect免自动输入密码登陆远程服务器 1.前提必须已经安装expect 2.新建login.sh,文件内容如下 #!/usr/bin/expect -f spawn ssh root@140. ...

  6. 无需Java代码通过JHipster生成有安全验证的微服务应用

    让我们继续登录到我们的应用程序,并导航到Account>Login菜单项.我们将使用admin/admin作为凭据,缺省情况下,JHipster将自动创建.一切进展顺利.欢迎页面将显示确认登录成 ...

  7. Redis面试题及答案整理

    1.什么是Redis?简述它的优缺点? Redis的全称是:Remote Dictionary.Server,本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库统统加载 ...

  8. VS2015常用快捷键

    1.回到上一个光标位置/前进到下一个光标位置  1)回到上一个光标位置:使用组合键“Ctrl + -”: 2)前进到下一个光标位置:“Ctrl + Shift + - ”. 2.复制/剪切/删除整行代 ...

  9. Blender 建模

    1.多图层切换 Blender也有图层的概念,我们在一个图层上建立了一个模型,可以在另外一个图层新建一个独立的模型.界面底部包含了Layer切换按钮.如下图所示: 当前我们正在操作第一个图层,如果想在 ...

  10. Hessian资料

    introduction http://www.cnblogs.com/hzmark/archive/2012/11/27/Hessian.html 超时时间设置 http://www.tuicool ...