forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpbtw.cpp
46 lines (46 loc) · 1.2 KB
/
helpbtw.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
// 2016-10-26
#include <climits>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
bool ok(const vector<int>& a, int range, int days, int target) {
vector<long long> ex(a.size());
long long cur = 0;
long long used = 0;
for (int i = 0; i < a.size(); i++) {
cur -= ex[i];
long long increment = target - a[i] - cur;
if (increment > 0) {
used += increment;
cur += increment;
if (i + range < a.size()) {
ex[i + range] += increment;
}
}
}
return used <= days;
}
int main() {
int T; scanf("%d", &T);
while (T--) {
int N, M, K; scanf("%d %d %d", &N, &M, &K);
vector<int> a(N);
for (int& x : a) scanf("%d", &x);
if (M == 0) {
// evil special case
printf("%d\n", *min_element(a.begin(), a.end()));
} else {
int l = 0, r = 2000000000;
while (r > l) {
int m = l + (r - l + 1)/2;
if (ok(a, M, K, m)) {
l = m;
} else {
r = m - 1;
}
}
printf("%d\n", l);
}
}
}