Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003
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.
想到了二分,但是没想到怎么处理记录每盆花的高度,很巧妙,pls表示当前花由前面的增加了多少,save记录这个点是不是正好w以外的点
1 #include <cstring> 2 3 #include <iostream> 4 5 #include <algorithm> 6 7 #include <cstdio> 8 9 #include <cmath> 10 11 #include <map> 12 13 #include <cstdlib> 14 15 #define M(a,b) memset(a,b,sizeof(a)) 16 17 #define INF 0x3f3f3f3f 18 19 using namespace std; 20 21 22 23 int n,m,w; 24 25 int num[260005]; 26 27 int save[260005]; 28 29 30 31 bool check(int aim) 32 33 { 34 35 M(save,0); 36 37 int pls = 0; 38 39 int day = 0; 40 41 for(int i = 0;i<n;i++) 42 43 { 44 45 pls += save[i]; 46 47 int tem = num[i] + pls; 48 49 if(tem<aim) 50 51 { 52 53 int need = (aim-tem); 54 55 day+=need; 56 57 if(day>m) return false; 58 59 pls+=need; 60 61 save[i+w]-=need; 62 63 } 64 65 } 66 67 return true; 68 69 } 70 71 72 73 int main() 74 75 { 76 77 scanf("%d%d%d",&n,&m,&w); 78 79 int min = INF; 80 81 int max = -INF; 82 83 for(int i = 0 ; i<n; i++) 84 85 { 86 87 scanf("%d",&num[i]); 88 89 if(num[i]<min) min = num[i]; 90 91 if(num[i]>max) max = num[i]; 92 93 } 94 95 int l = min; 96 97 int r = max + m; 98 99 int ans = 0; 100 101 while(l<=r) 102 103 { 104 105 int mid = (l+r)/2; 106 107 //cout<<l<<' '<<r<<endl; 108 109 if(check(mid)) 110 111 { 112 113 ans = mid; 114 115 l = mid+1; 116 117 } 118 119 else 120 121 r= mid-1; 122 123 } 124 125 printf("%d\n",ans); 126 127 return 0; 128 129 }