CF460C Present (二分 + 差分数列)

时间:2022-04-11 05:44:44

Codeforces Round #262 (Div. 2) C

C - Present

C. Present
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Sample test(s)
Input
6 2 3
2 2 2 2 1 1
Output
2
Input
2 5 1
5 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盆,初始碉值为a1,a2,...,an。每天可以增加连续的w盆花的碉值1点,进行m天,求碉值最低的花的碉值最大值。

题解:二分答案,用差分数列O(n)判断是否可行。

二分答案,设当前答案为x,也就是碉值最低的话的碉值最大值为x。

从头到尾观察花,若a[i]<x,则对a[i]开头的w盆花怒浇(x-a[i])天,让其碉值达到x。让所有的a[i]都>=x。若怒浇的天数和小于等于m,则可行。

而这个怒浇操作可以用差分队列实现,差分数列介绍在这里有:http://www.cnblogs.com/yuiffy/p/3923018.html

因为差分数列b[i]=a[i]-a[i-1],则当前点的值为now,下一个点的值就为now+b[i+1]。差分数列的L到R全加D操作:    b[L]+=x; b[R+1]-=x;(注意这题R可能会怒超边界,记得特殊处理一下或者数组开大点)

代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) prllf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back
vector<int>v;
int n,m,w;
int a[];
int b[]; void update(int L, int R, int x){
b[L]+=x;
if(R+<n) b[R+]-=x;
} bool check(int x){
int i,j;
for(i=;i<n;i++){
b[i]=a[i]-a[i-];
}
int anow=a[];
int y=;
for(i=;i<n;i++){
if(anow<x){
update(i,i+w-,x-anow);
y+=x-anow;
if(y>m)break;
anow=x;
}
anow+=b[i+];
}
if(y>m)return ;
else return ;
} int main(){
int i,j,k,mi;
scanf("%d%d%d",&n,&m,&w);
mi=1e9+;
REP(i,n){
scanf("%d",&a[i]);
mi=min(a[i],mi);
}
int l,r,mid;
l=mi;r=mi+m;
while(l<=r){
mid=(r-l)/+l;
if(check(mid))l=mid+;
else r=mid-;
}
printf("%d\n",r);
return ;
}