1. 程式人生 > >zoj4062 Plants vs. Zombies 二分+模擬(貪心的思維)

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=0,f=1; char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='
-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;} const int maxn=100010; int n; ll a[maxn],c[maxn],m; inline bool judge(ll high){ CLR(c,0); ll temp=m; if(m==0)return false; c[1]=a[1],m--; int i=1; for(;i<=n;i++) { if(m<=0)break; if(c[i]>=high) { if(m>0) { c[i+1]=a[i+1]; m--; continue; }else{ break; } } ll tmp=(ll)ceil((high-c[i])*1.0/a[i]); if(m>2*tmp){ m-=2*tmp+1; c[i]+=a[i]*tmp; c[i+1]+=a[i+1]*(tmp+1); continue; }else if(m==2*tmp){ c[i]+=a[i]*tmp; c[i+1]+=a[i+1]*tmp; break; }else{ break; } } m=temp; for(i=1;i<=n;i++) { if(c[i]<high)return false; } return true; } int main(){ int t; cin>>t; while(t--) { cin>>n>>m; ll l=0,r=0,mid,ans; for(int i=1;i<=n;i++){ a[i]=read(); r=max(r,a[i]*m); } if(m==0){ printf("0\n"); continue; } while(l<=r) { mid=(l+r)>>1; // printf("mid %d\n",mid); if(judge(mid)) { ans=mid; l=mid+1; }else{ r=mid-1; } } printf("%lld\n",ans); } }
View Code
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.