There are m days left to the birthday. The height of thei-th flower (assume that the flowers in the row are numbered from1 to n from left to right) is equal toai at the moment. At each of the remainingm days the beaver can take a special watering and waterw 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?
The first line contains space-separated integers n,m and w(1 ≤ w ≤ n ≤ 105; 1 ≤ m ≤ 105). The second line contains space-separated integersa1, a2, ..., an(1 ≤ ai ≤ 109).
Print a single integer — the maximum final height of the smallest flower.
6 2 3 2 2 2 2 1 1
2
2 5 1 5 8
9 题目大意: N朵花排成一列,已知初始高度a[i],一共可以浇M次水,每次只能选择一个宽度为w的区间浇水,花每被浇一次水长高一个单位,要让最矮的花最高。 思路:最小值最大问题,二分最终结果。 每次求出每朵花与目标高度的差,然后向右浇水。#include <iostream> #include <string> #include <string.h> #include <stdio.h> using namespace std; int d[300005]; int a[100005]; int n,m,w; bool check(int x) { memset(d,0,sizeof(d)); int num=m; int tem=0; int h; for (int i=1;i<=n;i++) { tem+=d[i]; h=a[i]+tem; if (h<x) { d[i+1]+=x-h; d[i+w]-=x-h; num-=x-h; } if (num<0) return 0; } return true; } int main() { int ans,max; while (~scanf("%d%d%d",&n,&m,&w)) { max=0; for (int i=1;i<=n;i++) { scanf("%d",&a[i]); if (max<a[i]) max=a[i]; } int l=1,r=max+m; while (l<=r) { int mid=(l+r)/2; if (check(mid)) { ans=mid; l=mid+1; } else r=mid-1; } printf("%d\n",ans); } return 0; }