[2016-02-05][CF][460C][Present]

[2016-02-05][CF][460C][Present]
CodeForces - 460C
Time Limit: 2000MSMemory Limit: 262144KB64bit IO Format: %I64d & %I64u

 Status

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 nfrom 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 waterw 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 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.


  • 时间:2016-01-27 12:49:31 星期三
  • 题目编号:CF 460C
  • 题目大意:
  • 分析:
  • 方法:二分
  • 解题过程遇到问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;
#define CLR(x,y) memset((x),(y),sizeof((x)))
#define getint(x) int (x);scanf("%d",&(x))
#define get2int(x,y) int (x),(y);scanf("%d%d",&(x),&(y))
#define get3int(x,y,z) int (x),(y),(z);scanf("%d%d%d",&(x),&(y),&(z))
#define getll(x) LL (x);scanf("%I64d",&(x))
#define get2ll(x,y) LL (x),(y);scanf("%I64d%I64d",&(x),&(y))
#define get3ll(x,y,z) LL (x),(y),(z);scanf("%I64d%I64d%I64d",&(x),&(y),&(z))
#define getdb(x) double (x);scanf("%lf",&(x))
#define get2db(x,y) double (x),(y);scanf("%lf%lf",&(x),&(y))
#define get3db(x,y,z) double (x),(y),(z);scanf("%lf%lf%lf",&(x),&(y),&(z))
 
#define getint2(x) scanf("%d",&(x))
#define get2int2(x,y) scanf("%d%d",&(x),&(y))
#define get3int2(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define getll2(x) scanf("%I64d",&(x))
#define get2ll2(x,y) scanf("%I64d%I64d",&(x),&(y))
#define get3ll2(x,y,z) scanf("%I64d%I64d%I64d",&(x),&(y),&(z))
#define getdb2(x) scanf("%lf",&(x))
#define get2db2(x,y) scanf("%lf%lf",&(x),&(y))
#define get3db2(x,y,z) scanf("%lf%lf%lf",&(x),&(y),&(z))
 
#define getstr(str) scanf("%s",str)
#define get2str(str1,str2) scanf("%s",str1,str2)
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);(x)--)
#define FOR2(x,y,z) for((x)=(y);(x)<(z);(x)++)
#define FORD2(x,y,z) for((x)=(y);(x)>=(z);(x)--)
const int maxn = 1E5 + 100;
int n , m , w;
int a[maxn];
int pre[maxn];
int check(int x){
    CLR(pre,0);
    int dh = 0,cnt = 0;
    FOR(i,0,n){
        dh += pre[i];
        if(a[i] + dh < x){
            int tmp = x - a[i] - dh;
            if(i + w <= n)
                pre[i + w] -= tmp;
            cnt += tmp;
            dh += tmp;
            if( cnt > m )
                return 0;
        }
    }
    return 1;
}
 
int main(){
    int l = 1E9, r = 0, mid;
    get3int2(n,m,w);
    FOR(i,0,n){
        getint2(a[i]);
        if( l > a[i] )   l = a[i];
    }
     
    r = l + m + 1;
    while(r - l > 1){
        mid = l + r >> 1;
        if(check(mid))  l = mid;
        else r = mid;
    }
    if(check(l))    printf("%d\n",l);
    else printf("%d\n",r);
    return 0;
}



来自为知笔记(Wiz)


posted on 2016-02-05 00:50  红洋  阅读(339)  评论(0)    收藏  举报

导航