ZOJ 4062(2018ICPC青岛E) Plants vs. Zombies 思维+二分答案

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

题意:给你一个数轴,有n个点表示有n个植物编号为从1到n,你起点在0,每个点有机器人每路过一次就可以浇一次水,每次浇水植物都会增加ai点防御,初始防御值都是0,机器人一共可以移动m步,最后整体的防御取n个中最小的那个,问你最后的带最大的防御是多少?,可以移动出1到n的范围,但是不会增加防御值。

思路:要是最小的防御值最大,那么最小的那个一定要变大,不然是无法提升的防御力力的,想要提升防御力最小的那个防御力而使最小的移动步数,所以最好还是挨个使当前最大,顺便照顾后面的,很容易想到二分答案,从1到n走,当前的防御值不满足时往后来回走,顺便给后面的浇水,满足了当前的后面的也有一定的防御力了,达到了最优。

注意:不是一定要枚举到最后一个的,如果在浇水使倒数第二个满足的时候,已经让最后一个满足了,就不需要在走到最后一个了。

代码:

#include<stdio.h>
#include<string.h>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#define ll long long
#define qq printf("QAQ\n");
using namespace std;
const int maxn=2e5+5;
const int inf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const double e=exp(1.0);
const double pi=acos(-1);
const double eps=1e-6;
int a[maxn],n;
ll m;
bool check(ll mid)
{
	ll now=0,next=0,cnt=0,num=0;
	for(int i=1;i<=n;i++)//每次考虑 
	{
		now=next;
		next=0;
		if(i==n&&now>=mid){//最后那个已经满足 不需要在哎继续走 
			continue;
		}
		if(now+a[i]>=mid){
			num++;
			if(num>m)return 0;
			continue;
		}
		cnt=(mid-now)/a[i]+((mid-now)%a[i]>0);
		num+=cnt*2-1;
		next=a[i+1]*(cnt-1);
		if(num>m)return 0;
	}
	return 1;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%lld",&n,&m);
		
		for(int i=1;i<=n;i++)scanf("%d",&a[i]);
		
		ll l=0,r=linf,mid,ans=0;
		
		while(l<=r)
		{
			mid=(r+l)>>1;
			if(check(mid))ans=mid,l=mid+1;
			else r=mid-1;
		}
		printf("%lld\n",ans);
	}
	return 0;
}
/*
200
3 5
100 10 10
*/

猜你喜欢

转载自blog.csdn.net/swust5120166213/article/details/85197906