#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pdi = pair<double, int>;
#define pb push_back
#define ff first
#define ss second
struct ST{
vector<int> t;
int n;
ST(int ns){
n = ns;
t.resize(4 * n);
}
void upd(int v, int tl, int tr, int& p, int& x){
if (tl == tr){
t[v] += x;
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
if (p <= tm){
upd(vv, tl, tm, p, x);
}
else {
upd(vv + 1, tm + 1, tr, p, x);
}
t[v] = max(t[vv], t[vv + 1]);
}
void upd(int p, int x){
upd(1, 1, n, p, x);
}
int get(){
return t[1];
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> a(n + 1);
vector<ll> p(n + 1);
for (int i = 1; i <= n; i++){
cin>>a[i];
p[i] = p[i - 1] + a[i];
}
if (n > 2e3) return 0;
vector<vector<double>> X(n + 1, vector<double>(n + 1));
for (int i = 1; i <= n; i++){
for (int j = i; j <= n; j++){
X[i][j] = 1.0 * (p[j] - p[i - 1]) / (j - i + 1);
}
}
priority_queue<pdi, vector<pdi>, greater<pdi>> pq;
for (int i = 1; i <= n; i++) pq.push({X[1][n - i + 1], i});
vector<int> t(n + 1, 1);
vector<pii> all = {{0, 0}};
int j = 1;
ST T(n);
double out = 1e9 + 1;
while (!pq.empty()){
auto [V, i] = pq.top(); pq.pop();
int r = t[i] + n - i;
if (t[i] == 1){
for (int j = 1; j <= r; j++){
T.upd(j, 1);
}
}
else {
T.upd(r, 1);
}
if (r < n){
pq.push({X[t[i] + 1][r + 1], i});
}
all.pb({t[i]++, r});
while (T.get() == n && j < all.size()){
auto [l, r] = all[j];
if (r == n) break;
int ii = n - (r - l);
if (l == (t[ii] - 1)){
for (int f = t[ii] - 1; f <= t[ii] - 1 + n - ii; f++){
T.upd(f, -1);
}
}
else {
T.upd(l, -1);
}
if (T.get() != n){
if (l == (t[ii] - 1)){
for (int f = t[ii] - 1; f <= t[ii] - 1 + n - ii; f++){
T.upd(f, 1);
}
}
else {
T.upd(l, 1);
}
break;
}
j++;
}
if (T.get() == n){
auto [l, r] = all[j];
out = min(out, V - X[l][r]);
}
}
cout<<fixed<<setprecision(12)<<out<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |