1. 程式人生 > >4062 Plants vs. Zombies 18青島站 二分

4062 Plants vs. Zombies 18青島站 二分

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

題解:二分列舉答案,先找出每個位置最少走過的次數,若大於1,我們讓他 先向右在回來,依次進行,最後一個點稍加判斷一下就可以了

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=1e5+10;
ll n,a[N],b[N];
ll m;
bool judge(ll x)
{
	ll sum=m;
	for(int i=1;i<=n;i++) b[i]=(x+a[i]-1)/a[i];
	for(int i=1;i<n;i++)
	{
		sum--;
		b[i]--;
		if(b[i]<=0) continue;
		sum-=b[i]*2;
		b[i+1]-=b[i];
		if(sum<0) return 0;
	}
	if(b[n]>0)
	{
		sum--;
		b[n]--;
		sum-=b[n]*2; 
	}
	if(sum<0) return 0;	
	return 1;
} 
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%lld%lld",&n,&m);
		for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
		ll l=0,r=1e12;
		ll ans=0;
//		judge(7);
		while(l<=r)
		{
			ll mid=(r+l)>>1;
		//	cout<<l<<" "<<r<<" "<<mid<<endl;
			if(judge(mid))
			{
				l=mid+1;
				ans=max(ans,mid);	
			}
			else r=mid-1;
		}
		printf("%lld\n",ans);
	}
	return 0;
 }