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
*/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章