-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path460-C.cpp
60 lines (49 loc) · 1.15 KB
/
460-C.cpp
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
/*
miscellaneous > binary search > on answer
difficulty: hard
date: 06/Jan/2020
by: @brpapa
*/
#include <iostream>
#include <vector>
using namespace std;
int N, M, W;
vector<int> hgt;
bool can(int ans) {
int m = 0;
vector<int> wt(N, 0); // wt[i] = qte de regadas que inicia na i-ésima flor
int curr = 0; // incremento à hgt[i], considerando as regadas anteriores que ainda atinge a flor i
for (int i = 0; i < N; i++) {
if (i - W >= 0)
curr -= wt[i - W]; // decrementa regadas anteriores que não alcança a flor i
if (hgt[i] + curr < ans) {
wt[i] = ans - (hgt[i] + curr);
curr += wt[i];
m += wt[i];
if (m > M)
return false;
}
}
return true;
}
int main() {
cin >> N >> M >> W;
int ans, low = 1e9, high = 1;
hgt.resize(N);
for (int &h : hgt) {
cin >> h;
low = min(low, h);
high = max(high, h + M);
}
while (low <= high) {
int mid = (low + high) >> 1;
if (can(mid)) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
cout << ans << endl;
return 0;
}