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?
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).
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
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.
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 2e5 + 10 , M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, m, w;
int a[N];
int v[N];
bool check(int len)
{
MS(v, 0);
int use = 0;
int val = 0;
for (int i = 1; i <= n; ++i)
{
val += v[i];
if (a[i] + val < len)
{
int need = len - (a[i] + val);
use += need;
if (use > m)return 0;
val += need;
v[i + w] -= need;
}
}
return 1;
}
int main()
{
while (~scanf("%d%d%d", &n,&m,&w))
{
int l = 1e9;
int r = 1e9 + m;
for (int i = 1; i <= n; ++i)
{
scanf("%d", &a[i]);
gmin(l, a[i]);
}
while (l < r)
{
int mid = (l + r + 1) >> 1;
if (check(mid))l = mid;
else r = mid - 1;
}
printf("%d\n", l);
}
return 0;
}
/*
【trick&&吐槽】
1,使得最小的尽可能大,显然就是二分。
2,细节不要写错了。
【题意】
有一排花,n(1e5)朵。
每朵花都有一个成长度a[](1<=a[]<=1e9)
我们可以浇花m(1e5)次,每次连续的长度为w(1e5)的一段,
我们想使得最小成长度的花的成长度尽可能高,输出。
【类型】
二分 贪心
【分析】
显然可以二分答案+贪心。
然后就没有然后了。
注意二分细节,防止爆int
*/