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$ 个植物,编号为 $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]的更多相关文章
- 2018 青岛ICPC区域赛E ZOJ 4062 Plants vs. Zombie(二分答案)
Plants vs. Zombies Time Limit: 2 Seconds Memory Limit: 65536 KB BaoBao and DreamGrid are playin ...
- ZOJ 4062 Plants vs. Zombies(二分答案)
题目链接:Plants vs. Zombies 题意:从1到n每个位置一棵植物,植物每浇水一次,增加ai高度.人的初始位置为0,人每次能往左或往右走一步,走到哪个位置就浇水一次.求m步走完后最低高度的 ...
- 【非原创】ZOJ - 4062 Plants vs. Zombies【二分】
题目:戳这里 题意:机器人走过一个花,可以给那个花浇水,给定步数下,问花的最小的最大能量值. 学习博客:戳这里 本人代码: 1 #include <bits/stdc++.h> 2 typ ...
- 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$), ...
- 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$,现在你有 ...
- 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 ...
- 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 ...
- 2020 ICPC Asia Taipei-Hsinchu Regional Problem H Optimization for UltraNet (二分,最小生成树,dsu计数)
题意:给你一张图,要你去边,使其成为一个边数为\(n-1\)的树,同时要求树的最小边权最大,如果最小边权最大的情况有多种,那么要求总边权最小.求生成树后的所有简单路径上的最小边权和. 题解:刚开始想写 ...
- 2019 ICPC Asia Taipei-Hsinchu Regional Problem K Length of Bundle Rope (贪心,优先队列)
题意:有\(n\)堆物品,每次可以将两堆捆成一堆,新堆长度等于两个之和,每次消耗两个堆长度之和的长度,求最小消耗使所有物品捆成一堆. 题解:贪心的话,每次选两个长度最小的来捆,这样的消耗一定是最小的, ...
随机推荐
- MongoDB Notes
MongoDB 启动一个 mongo 实例 $ docker run --name some-mongo -d daocloud.io/mongo 由于该镜像的 Dockerfile 中包含了 EXP ...
- 从零开始搭建FAQ引擎--基于ES的字面匹配
从零开始搭建FAQ引擎--基于ES的字面匹配
- 【转载】最强NLP预训练模型!谷歌BERT横扫11项NLP任务记录
本文介绍了一种新的语言表征模型 BERT--来自 Transformer 的双向编码器表征.与最近的语言表征模型不同,BERT 旨在基于所有层的左.右语境来预训练深度双向表征.BERT 是首个在大批句 ...
- 解决ubuntu系统中firefox无法播放网页版音乐播放器音乐
Reference: https://blog.csdn.net/h736131708/article/details/80775382 因为网页版的qq音乐或者网易云音乐都把音频换成了AAC格式,这 ...
- hdoj:2072
单词数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 【转】IT大牛博客
原文:http://blog.csdn.net/qq1175421841/article/details/49384841 首届中国最受欢迎50大技术博客获奖名单如下: 第一名:李会军 http:/ ...
- SAP从入门到精通 知识体系
初步认识 SAP标准课程培训 详细信息点击: SAP从入门到精通课程 标准培训主要是基于SAP标准的课程架构,定期在SAP的培训中心面向广大SAP客户开设公开课.培训过程中会应用SAP标准的教材内容, ...
- win2003远程桌面怎么切换到多用户?
怎么用远程桌面切换server2003的多用户的问题?server2003操作系统的远程桌面是多用户的,就是你的机子远程桌面到服务器而其它机子也可以远程桌面,所以往往有时候你看不到原始的桌面的样子,所 ...
- SpringMVC+ Mybatis 配置多数据源 + 自动数据源切换 + 实现数据库读写分离
现在大型的电子商务系统,在数据库层面大都采用读写分离技术,就是一个Master数据库,多个Slave数据库.Master库负责数据更新和实时数据查询,Slave库当然负责非实时数据查询.因为在实际的应 ...
- A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' or 'http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider' was not present on the pro
Try setting (in global.cs): AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier; ...