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

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;
}

原文地址:https://www.cnblogs.com/shenben/p/10312053.html

时间: 2024-08-29 22:39:41

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

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

Ryuji doesn&#39;t want to study 2018徐州icpc网络赛 树状数组

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That means, if he reads books from ll to rr, he will get a

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

这是2014年上海区域赛的一道水题.请原谅我现在才发出来,因为我是在太懒了.当然,主要原因是我刚刚做出来. 其实去年我就已经看到这道题了,因为我参加的就是那一场.但是当时我们爆零,伤心的我就再也没有看过那一场的题了.昨天我的队友的高中同学建议我们一起来打一打这场比赛吧,然后我才再次回顾这场比赛.结果一堆琐事,我一共也没有做多久的题,我的队友扎扎实实看了5个小时的题,把另一道水题给过了.全场我们也只过了那么一道题.学姐说,做重现赛和现场赛比较,需要去掉一题,那么我们又爆零了. 题意: 我方有n个人

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

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4061 题意:定义一个长度为 n 的序列 a1,a2,..,an 和长度为 m 的序列 b1,b2,..,bm 所构成的新序列 c 为 a1b1,a1b2,....,anbm,给出最终的序列和两个初始序列的长度,构造出字典序最小的初始序列. 题解:首先我们知道两个个位数相乘最多可以得到两位数,易知最终序列的第一个数字 c1 的构造一定有 a1 的参与,当 a1 <

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

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4068 题意:吃鸡游戏简化为二维平面上有 n 个人 (xi,yi),空投的位置在 (x0,y0),每一秒所有人向靠近空投的位置走一步,四个方向有优先级先后(优先纵坐标),若已经在空投的位置则不变.往空投位置移动过程中,若两个或者更多人在同一点相遇(在空投相遇不算),则他们都死亡.给出空投的纵坐标,询问空投在所有横坐标中最少和最多存活的人数是多少. 题解:在最优情况

2018南京icpc现场赛心得

第一次参加icpc的比赛,也是第一块奖牌,虽然只是铜,但其实打的已经很好了,稍微差一点就可以摸银了. 之前参加省赛,成为那次比赛我校唯一一个没拿奖的队伍,其实还是一直都有一些心结的,而这段时间和新的队友的组队,虽然新队员的实力比之前的队友抢了很多,但是总感觉配合一直有问题,在比赛前一天的晚上,睡在一个房间里聊了很多话,也算是和队友真正达成了默契吧,所以第二天的现场赛配合的很好. 但是我自己在最近一段时间由于各种原因,其实个人的训练远没有之前那么多了,希望接下来能调整自己的训练,争取能在青岛站给奖

Trace 2018徐州icpc网络赛 思维+二分

There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy) means the wave is a rectangle whose vertexes are ( 00 , 00 ), ( xx , 00 ), ( 00 , yy ), ( xx , yy ). Every time the wave will wash out the trace of f

2014ACM/ICPC区域赛西安站总结

我的第一场比赛就这样结束了,感受很丰富,百感交集啊! 不说自己水平怎么样了,就说一句吧,很水...各种知识各种能力都待提高啊. 一.参赛准备 自己真正接触ACM还是在暑假,自己在队里面可以说是练ACM最晚的了,很感谢老师能相信我让我出去比赛,这是我万万没想到的事.内心不想辜负教主的信任,比赛前两周,自己拼命做了各个赛区两三道能出的题,希望能赛前找找比赛的感觉. 在去西安的路上,心里一直很担心,在担心自己的第一场比赛会打成什么样,在路上和hsk和gmc聊了聊他们打省赛的事,想想自己从来没有打过比赛

2017 ICPC区域赛(西安站)--- J题 LOL(DP)

题目链接 problem description 5 friends play LOL together . Every one should BAN one character and PICK one character . The enemy should BAN 55 characters and PICK 55 characters . All these 2020 heroes must be different . Every one can BAN any heroes by h