zoj4062 Plants vs. Zombies 二分+模拟(贪心的思维)
题目大意:有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=,f=;
char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;}
const int maxn=;
int n;
ll a[maxn],c[maxn],m;
inline bool judge(ll high){
CLR(c,);
ll temp=m;
if(m==)return false;
c[]=a[],m--;
int i=;
for(;i<=n;i++)
{
if(m<=)break;
if(c[i]>=high)
{
if(m>)
{
c[i+]=a[i+];
m--;
continue;
}else{
break;
}
}
ll tmp=(ll)ceil((high-c[i])*1.0/a[i]);
if(m>*tmp){
m-=*tmp+;
c[i]+=a[i]*tmp;
c[i+]+=a[i+]*(tmp+);
continue;
}else if(m==*tmp){
c[i]+=a[i]*tmp;
c[i+]+=a[i+]*tmp;
break;
}else{
break;
}
}
m=temp;
for(i=;i<=n;i++)
{
if(c[i]<high)return false;
}
return true;
}
int main(){
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
ll l=,r=,mid,ans;
for(int i=;i<=n;i++){
a[i]=read();
r=max(r,a[i]*m);
}
if(m==){
printf("0\n");
continue;
} while(l<=r)
{
mid=(l+r)>>;
// printf("mid %d\n",mid);
if(judge(mid))
{
ans=mid;
l=mid+;
}else{
r=mid-;
}
}
printf("%lld\n",ans);
}
}
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.
zoj4062 Plants vs. Zombies 二分+模拟(贪心的思维)的更多相关文章
- 2018ICPC青岛 E - Plants vs. Zombies (二分+模拟)
ZOJ - 4062 题意:有n个植物排成一排,按顺序植物的编号是1-n,每个植物都有一个生长速率,有一个机器人,机器人可以走m步,每走一步,这个机器人就会浇一次水,浇一次水那个植物就会长 自身的生长 ...
- ZOJ4062 Plants vs. Zombies(二分+贪心)
题目链接:传送门 题目大意: 有n棵植物依次放在1-n,机器人从0出发浇水,每棵植物被浇水时di += ai,求浇m次水后min{di|1 ≤ i ≤ n}的最大值.(浇水时必须往左或往右走一步,落脚 ...
- 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$ ...
- ZOJ 4062 Plants vs. Zombies(二分答案)
题目链接:Plants vs. Zombies 题意:从1到n每个位置一棵植物,植物每浇水一次,增加ai高度.人的初始位置为0,人每次能往左或往右走一步,走到哪个位置就浇水一次.求m步走完后最低高度的 ...
- Plants vs. Zombies(二分好题+思维)
Plants vs. Zombies http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5819 BaoBao and DreamG ...
- 2018 青岛ICPC区域赛E ZOJ 4062 Plants vs. Zombie(二分答案)
Plants vs. Zombies Time Limit: 2 Seconds Memory Limit: 65536 KB BaoBao and DreamGrid are playin ...
- uva 12452 Plants vs. Zombies HD SP (树DP)
Problem I: Plants vs. Zombies HD Super Pro Plants versus Zombies HD Super Pro is a game played not a ...
- CodeForces - 363D --二分和贪心
题目:CodeForces - 363D 题意:给定n个学生,其中每个学生都有各自的私己钱,并且自己的私己钱只能用在自己买自行车,不能给别人. 给定m个自行车,每个自行车都有一个价格. 给定公有财产a ...
- 【BZOJ1816】[CQOI2010]扑克牌(二分,贪心)
[BZOJ1816][CQOI2010]扑克牌(二分,贪心) 题面 BZOJ 题解 看了一眼这题,怎么这么眼熟?woc,原来\(xzy\)的题目是搬的这道啊... 行,反正我考的时候也切了,这数据范围 ...
随机推荐
- webservice CXF 相关面试题
Web Service的优点(1) 可以让异构的程序相互访问(跨平台)(2) 松耦合(3) 基于标准协议(通用语言,允许其他程序访问) 1:WEB SERVICE名词解释.JSWDL开发包的介绍.JA ...
- SqlServer——用户定义函数
根据用户定义函数返回值的类型,可将用户定义函数分为如下三个类别: (1) 返回值为可更新表的函数 若用户定义函数包含单个 SELECT 语句且该语句可更新,则该函数返回的表也可更新,这样的函数称为内嵌 ...
- Solidity智能合约升级解决方案
https://blog.zeppelin.solutions/proxy-libraries-in-solidity-79fbe4b970fd
- R 如何 隐藏坐标轴
x = c(7,5,8)dim(x)<-3names(x)<-c("apple","banana", "cherry")plot ...
- 替归算法获取Treeview所有节点
treeview.nodes是获取下一级所有子节点,但是如果是多层的话,就不能,想个法子来获取所有的节点(含节点的子节点),想了想 还是替归算法比较方便,如是有了下面的代码 public static ...
- 构建空Datatable
方法一 DT = new DataTable(); DataRow DR = DT.NewRow(); //构建列(名称,数据类型) DT.Columns.Add("代码", Ty ...
- .Net中的并行编程-1.路线图(转)
大神,大神,膜拜膜拜,原文地址:http://www.cnblogs.com/zw369/p/3834559.html 目录 .Net中的并行编程-1.路线图 分析.Net里线程同步机制 .Net中的 ...
- .net core in Docker 部署方案(随笔)
前一段时间由于项目需要 .net core 在docker下的部署,途中也遇到很多坑,看了各同行的博客觉得多多少少还是有些问题,原本不想写此篇文章,由于好友最近公司也需要部署,硬是要求,于是花了些时间 ...
- 自建脚手架之配置中心--LightConf的实现
常规项目开发过程中, 通常会将配置信息位于在项目resource目录下的properties文件文件中, 配置信息通常包括有: jdbc地址配置.redis地址配置.活动开关--等等.因此每次上线或者 ...
- C++ TIM或者QQ 自动发送消息
简单写了一下 很简单的demo 闲着没事干 #include "stdafx.h" #include <thread> #include <Windows.h&g ...