Present - CODEFORCES, 460c 二分 扫描线求和

来源:互联网 发布:轻淘客和淘宝联盟区别 编辑:程序博客网 时间:2024/05/18 02:29

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 nm 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.

Sample test(s)
Input
6 2 32 2 2 2 1 1
Output
2
Input
2 5 15 8
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朵花,每朵花都有一个输入的初始高度,还剩下m天,每天可以给连续w朵花浇水,每天只能浇一次,每浇一次花长一个单位长度,求m天所有花中最矮的花最大高度。

解题思路:二分最后高度,对于每一个高度判断所有花是否都能满足。判断时从左往右依次判断,如果每次给连续的w朵花都加1,效率为n*w = 10^10会超时。用一个temp变量表示当前花朵已经被浇了多少次,用一个数组(初值都为0)表示temp中有多少次对它不起作用。



假设此时二分高度为 h=6, 每朵花初始高度依次如图所标。首先给第一朵花浇水,当前高度为2,还需要浇4天,temp = temp + 4,temp=4,但这四天只能对1,2,3起作用,对4不起作用,所以c[4] = -4,表示前面累积天数有四天对花4不起作用。给第二朵花浇水时,temp=4,已经浇了四天,而他只需要两天所以不用再浇,3也一样,c[2+w]=c[5]=0, c[3+w] = c[6]=0, 到4时temp = 4, 先消除前面影响,temp += c[4], temp = 0. 所以还需再浇4天,temp = 4,依次往后推。。。最后看总共浇水天数与剩下天数相比较 判定该高度是否可行。

代码:

#include <cstdio>#include <iostream>#include <cstring>using namespace std;#define maxn 100000+10#define INF 0x3fffffff/////////////////////////////int n,m,w;int a[maxn];/////////////////////bool ok(int k){    int temp = 0,b[maxn],q,cnt=0;    memset(b,0,sizeof(b));    for(int i=0;i<n;i++){        q = k - a[i];        temp += b[i];        if(q>temp){            cnt += q - temp;            if(cnt>m) return false;            if(i+w<n) b[i+w] = -(q-temp);            temp = q;        }    }//printf("k: %d  cnt:  %d\n",k,cnt);    return cnt <=m;}int main(){    cin >> n >> m >>w;    for(int i=0;i<n;i++){        scanf("%d",&a[i]);    }    int l = 0, r = INF, mid;    while(l<r){        mid = (l+r) >> 1;        if(ok(mid)) l = mid+1;        else r = mid;    }    cout << r - 1 <<endl; }



0 0