题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4062

题意:

现在在一条 $x$ 轴上玩植物大战僵尸,有 $n$ 个植物,编号为 $1 \sim n$,第 $i$ 个植物的位置在坐标 $i$,成长值为 $a_i$,初始防御值为 $d_i$。

现在有一辆小车从坐标 $0$ 出发,每次浇水操作必须是先走 $1$ 单位长度,然后再进行浇水,植物被浇一次水,防御值 $d_i+=a_i$。

现在知道,小车最多进行 $m$ 次浇水操作,而已知总防御值为 $min{d_1,d_2,d_3,\cdots,d_n}$。求最大的总防御值为多少。

Input

There are multiple test cases. The first line of the input contains an integer $T$, indicating the number of test cases. For each test case:

The first line contains two integers $n$ and $m$ ($2 \le n \le 10^5, 0 \le m \le 10^{12}$), indicating the number of plants and the maximum number of steps the robot can take.

The second line contains integers $a_1,a_2, \cdots, a_n$ ($1 \le a_i \le 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

题解:

二分总防御值,对于固定的防御值 $d$,显然花园里的每个植物都要不小于 $d$,因此贪心地从左往右寻找每一个防御值小于 $d$ 的植物,对它和它右侧的植物反复浇水。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+; int n;
ll m;
ll a[maxn],d[maxn]; ll judge(ll k)
{
memset(d,,(n+)*sizeof(ll));
ll cnt=;
for(int i=;i<=n;i++)
{
if(i==n && d[i]>=k) break;
cnt++, d[i]+=a[i];
if(d[i]>=k) continue; ll tmp=(k-d[i])/a[i]+((k-d[i])%a[i]>);
cnt+=*tmp;
d[i]+=tmp*a[i];
d[i+]+=tmp*a[i+];
}
return cnt;
} int main()
{
ios::sync_with_stdio();
cin.tie(); int T;
cin>>T;
while(T--)
{
cin>>n>>m;
ll mn=1e5+;
for(int i=;i<=n;i++) cin>>a[i], mn=min(mn,a[i]); ll l=, r=mn*m;
while(l<r)
{
ll mid=(l+r+)>>;
if(judge(mid)<=m) l=mid;
else r=mid-;
}
cout<<l<<'\n';
}
}

注:

这题的右区间不要直接设成 $1e17$,因为这样当 $a[i]$ 都是小数据的时候 $judge$ 函数里的 $cnt$ 会爆long long。

不妨设成 $a_i$ 最小值的 $m$ 倍,很容易证明最后的答案是不会超过这个值的。

ZOJ 4062 - Plants vs. Zombies - [二分+贪心][2018 ACM-ICPC Asia Qingdao Regional Problem E]的更多相关文章

  1. 2018 青岛ICPC区域赛E ZOJ 4062 Plants vs. Zombie(二分答案)

    Plants vs. Zombies Time Limit: 2 Seconds      Memory Limit: 65536 KB BaoBao and DreamGrid are playin ...

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

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

  3. 【非原创】ZOJ - 4062 Plants vs. Zombies【二分】

    题目:戳这里 题意:机器人走过一个花,可以给那个花浇水,给定步数下,问花的最小的最大能量值. 学习博客:戳这里 本人代码: 1 #include <bits/stdc++.h> 2 typ ...

  4. ZOJ 4067 - Books - [贪心][2018 ACM-ICPC Asia Qingdao Regional Problem J]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4067 题意: 给出 $n$ 本书(编号 $1 \sim n$), ...

  5. ZOJ 4060 - Flippy Sequence - [思维题][2018 ACM-ICPC Asia Qingdao Regional Problem C]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4060 题意: 给出两个 $0,1$ 字符串 $S,T$,现在你有 ...

  6. ZOJ 4063 - Tournament - [递归][2018 ACM-ICPC Asia Qingdao Regional Problem F]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4063 Input Output Sample Input 2 3 ...

  7. ZOJ 4070 - Function and Function - [签到题][2018 ACM-ICPC Asia Qingdao Regional Problem M]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5840 Time Limit: 1 Second Mem ...

  8. 2020 ICPC Asia Taipei-Hsinchu Regional Problem H Optimization for UltraNet (二分,最小生成树,dsu计数)

    题意:给你一张图,要你去边,使其成为一个边数为\(n-1\)的树,同时要求树的最小边权最大,如果最小边权最大的情况有多种,那么要求总边权最小.求生成树后的所有简单路径上的最小边权和. 题解:刚开始想写 ...

  9. 2019 ICPC Asia Taipei-Hsinchu Regional Problem K Length of Bundle Rope (贪心,优先队列)

    题意:有\(n\)堆物品,每次可以将两堆捆成一堆,新堆长度等于两个之和,每次消耗两个堆长度之和的长度,求最小消耗使所有物品捆成一堆. 题解:贪心的话,每次选两个长度最小的来捆,这样的消耗一定是最小的, ...

随机推荐

  1. 整体C#与Sql培训内容及结构

    图如果看不清可以右键存图片到本地

  2. mysql 按照月份自动创建表,以年和月为表明,动态生成。

    需求:mysql5.5 数据库,想要根据月份自动创建表,每个月创建一张表,需要数据库自动创建,并根据当前年和月动态生成表名称. 解决办法:1 连接数据库工具为Navicat  2  首先创建存储过程, ...

  3. 物联网架构成长之路(28)-Docker练习之MQ中间件(Kafka)

    0. 前言 消息队列MQ,这个在一般的系统上都是会用到的一个中间件,我选择Kafka作为练手的一个中间件,Kafka依赖Zookeeper.Zookeeper安装上一篇博客已经介绍过了. 1. Kaf ...

  4. gsoap入门:C/C++代码生成及编译--包含soapcpp2 -qname添加命名空间后报错的解决方法--可用

    gsoap是什么 先来一段百度百科,说说gsoap是什么: gSOAP一种跨平台的开源的C/C++软件开发工具包.生成C/C++的RPC代码,XML数据绑定,对SOAP Web服务和其他应用形成高效的 ...

  5. GRE封装解封装过程

    GRE(Generic Routing Encapsulation,通用路由封装)协议是对某些网络层协议(IPX, AppleTalk, IP,etc.)的数据报文进行封装,使这些被封装的数据报文能够 ...

  6. top-N 抽样

        1, 使用hive标记random:(如果是mr,就自己标记random值) use ps; set mapred.job.priority=VERY_HIGH; set mapred.job ...

  7. c++ 异常 warning: 'MEMORY_UNIT_NAME' defined but not used

    是开关的问题  , 将 #-g -O2 -pipe -W -Wall -Werror -fPIC -Wno-deprecated    c++ 去掉.不检查.

  8. MyBatis 配置多数据源

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. PHP下载远程图片的3个方法

    From: http://blog.csdn.net/iefreer/article/details/46930239 直接上代码 <?php function dlfile1($file_ur ...

  10. tensorflow finuetuning 例子

    最近研究了下如何使用tensorflow进行finetuning,相比于caffe,tensorflow的finetuning麻烦一些,记录如下: 1.原理 finetuning原理很简单,利用一个在 ...