题目链接:https://codeforces.com/contest/460/problem/C
题干描述:
Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions.
There are m days left to the birthday. The height of the i-th flower (assume that the flowers in the row are numbered from 1 to n from left to right) is equal to ai at the moment. At each of the remaining m days the beaver can take a special watering and water w contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get?
Input
The first line contains space-separated integers n, m and w (1 ≤ w ≤ n ≤ 105; 1 ≤ m ≤ 105). The second line contains space-separated integers a1, a2, …, an (1 ≤ ai ≤ 109)
Output
Print a single integer — the maximum final height of the smallest flower.
Examples
Input
6 2 3
2 2 2 2 1 1
Output
2
Input
2
Output
9
Note
In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It’s impossible to get height 3 in this test.
题意描述
小明同学想要给他老师送生日礼物,他选择自己种花送给老师(怪有心嘞)。
花一共有n朵,排成一排,他每天可以浇对连续的w棵花浇一次。
花每被浇一次就会长高一个高度。
距离老师生日还有m天(小明还能浇m次花)。
目标是想要m天后,最矮的花的高度最大。
思路分析
假设初始状态下n朵花中高度最低的一朵的高度为min_height。那我们可以确定答案的取值范围是min_height ~ min_height + m。
最坏的情况就是min_height了,最好的情况也就是只需要对高度最低的花连浇m次,最后的高度是min_height + m。
对于min_height ~ min_height + m中的每一个高度都有两种可能,一种是其高度小于等于最终答案,一种是其高度大于最终答案。那我们就可以使用二分来解决了。
接下来描述,在二分的过程中对每个mid的判断过程。
遍历高度数组,当某棵花的高度小于mid时,就对自它开始的w朵花浇水。
当浇水的次数用完了,还没能把所有花都浇到mid高时,说明答案高度比mid低,我们向下找解。当把所有花都浇到mid高时,剩余浇水次数大于等于0时,说明答案大于等于mid,我们向上找解。
代码实现
#include<iostream>
#include<algorithm>
using namespace std;
const int SIZE = 1e5+5;
int a[SIZE],waterd[SIZE];
int n,m,w;
bool judge(int target);
int main(){
cin>>n>>m>>w;
int min_height = 1e9+10;
for(int i = 1;i <= n;i++){
cin>>a[i];
min_height = min(min_height,a[i]);
}
int max_height = min_height + m + 1;
while(min_height < max_height){
int mid = (min_height + max_height)/2;
if(judge(mid)) min_height = mid+1;
else max_height = mid;
}
cout<<min_height-1;
}
bool judge(int target){
int increa = 0;//当前点的浇水次数
int remain = m;//本次尝试剩余的浇水次数
for(int i = 1;i <= n;i++){
if(i > w) increa-=waterd[i-w];//超过了在第i-p个点的浇水的影响效果
waterd[i] = max(0 , target - a[i] - increa);//计算第i个点应该浇多少水
remain-=waterd[i];//剩余浇水的次数减去本次浇水的次数
increa+=waterd[i];//当前点的被浇次数加上本次浇水的次数
if(remain < 0) return false;//浇水次数成了负的,说明到不了该目标答案
}
return true;//可以到达该目标答案
}
若有差错,还望指出!
欢迎各位大佬交流指点。