-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop_counting.cpp
82 lines (61 loc) · 1.63 KB
/
stop_counting.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <bits/stdc++.h>
using namespace std;
#define llong long long int
#define ldouble long double
const ldouble EPS = 1e-9;
template<class T, class C = greater<T>>
struct MinQueue {
MinQueue() { clear(); }
void clear() {
id = 0;
q.clear();
}
void push(T x) {
pair<int, T> nxt(1, x);
while (q.size() > id && cmp(q.back().second, x)) {
nxt.first += q.back().first;
q.pop_back();
}
q.push_back(nxt);
}
T qry() { return q[id].second; }
void pop() {
q[id].first--;
if (q[id].first == 0) { id++; }
}
private:
vector<pair<int, T>> q;
int id;
C cmp;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << setprecision(12) << fixed;
int N; cin >> N;
vector<int> a(N + 1);
for (int i = 1; i <= N; i++)
cin >> a[i];
vector<llong> ps(2 * N + 1, 0LL);
for (int i = 1; i <= 2 * N; i++)
ps[i] = ps[i - 1] + a[i <= N ? i : i - N];
ldouble l = 0.0L, r = 2e9;
for (int i = 0; i < 200; i++) {
ldouble m = (l + r) / 2.0L;
MinQueue<ldouble> q;
for (int j = 1; j <= N; j++)
q.push(ps[j] - m * j);
bool possible = false;
for (int j = N + 1; j <= 2 * N && !possible; j++) {
ldouble v = ps[j] - m * j;
if (q.qry() - v <= EPS) possible = true;
q.pop();
}
if (possible) l = m;
else r = m;
}
ldouble ans = (l + r) / 2.0L;
for (int i = 0; i <= N; i++)
ans = max(ans, (ldouble) (ps[N] - ps[i]) / (N - i));
cout << ans << "\n";
}