Codeforces Round #262 (Div. 2)-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 wcontiguous 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.

Examples
input
6 2 3
2 2 2 2 1 1
output
2
input
2 5 1
5 8
output
9

二分枚举最小值,判断是否能达到

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#define maxn 100005
#define MOD 1000000007
#define INF 1E9
using namespace std;
typedef long long ll;

int n, m, w;
ll num[maxn];
int p[maxn];
bool judge(ll k){
	int d = m;
	for(int i = 1; i <= n; i++){
		p[i] += p[i-1];
		if(num[i] + p[i] < k){
			ll e = k - num[i] - p[i];
			if(e > d)
			 return false;
			p[i] += e;
			int h = i + w;
			if(h > n + 1)
			 h = n + 1;
			p[h] = -e;
			d -= e;
		}
	}
	return true;
}
int main(){
//	freopen("in.txt", "r", stdin);
	scanf("%d%d%d", &n, &m, &w);
	for(int i = 1; i <= n; i++)
	 scanf("%I64d", num+i);
	ll l = 0, r = 1e10;
	while(l < r){
		memset(p, 0, sizeof(p));
		ll mid = (l + r) >> 1;
		if(judge(mid))
			l = mid + 1; 
		else
		    r = mid;
	}
	cout << l - 1 << endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值