首页 > 代码库 > cf 460C

cf 460C

J - Present
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 460C
Appoint description: 

Description

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.

Sample Input

Input
6 2 3
2 2 2 2 1 1
Output
2
Input
2 5 1
5 8
Output
9

Hint

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 <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <vector>using namespace std;typedef long long ll;const ll maxn = 1e14;vector<ll> a;ll m, w, n;bool check(ll x){    ll move = 0;    ll scur = 0;    vector<ll> s(n+1, 0);    // s 的 n+1个元素初始值都为0    for (ll i = 0; i < n; i++)    {        scur -= (i >= w ? s[i-w] : 0);  // 刚开始的w-1朵花不能依靠前面花的被淋天数来增高,之后的都可以         if (x > a[i] + scur)        {            s[i] = x - scur- a[i];   // 第i朵花要达到x实际还需要x-scur-a[i]这么多天            scur += s[i];           // 更新次数,之后的w-1朵花可以减少淋scur那么多天            move += s[i];            // 累积天数        }        if (move > m)            return false;    }    return true;  // move <= m}int main(){    while (scanf("%lld%lld%lld", &n, &m, &w) != EOF)    {        a.resize(n+1);   // 设 a 的 size和capacity 为 n+1(其实n也可以)        for (int i = 0; i < n; i++)            scanf("%lld", &a[i]);        ll left = 1, right = maxn;        ll X = 0;        while (left <= right)  // < 是错的,一定是 <=        {            ll mid = (left+right) >> 1;            if (check(mid))     // 代表每朵花都可以在 m 天内达到mid的高度            {                X = mid;                left = mid + 1;            }            else                right = mid - 1;        }        printf("%lld\n", X);    }    return 0;}

  

cf 460C