题目传送门
题目大意:有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);} }
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.