和大家的思路一样,二分平均值,check 函数利用单调队列 O(n) 解决,总复杂度为 O(n log n).
代码:
using namespace std;
const int N = 1e7+5;
int n, m;
int s[N],a[N],S,T;
deque<int> q,emp;
bool check(double x) {
q = emp;
q.push_back(0) ;
for(int i = 1; i <= n; i++) {
while(q.front() + T < i) { q.pop_front();}
if(i - q.front() >= S && i - q.front() <= T) {
if((s[i]-s[q.front()])*1.0/(i-q.front()) >= x) return 1;
}
while(!q.empty() && s[q.back()] >= s[i]) { q.pop_back(); }
q.push_back(i);
}
return 0;
}
int main() {
scanf("%d%d%d", &n, &S, &T);
for(int i = 1; i <= n; i++) {
scanf("%d",a + i);
s[i] = s[i - 1] + a[i];
}
double l=-1e5,r=1e5;
while(l+0.0001<r) {
double mid=(l+r)/2;
if(check(mid)) l=mid;
else r=mid-1;
}
printf("%.3lf",l);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N = 1e7+5;
int n, m;
int s[N],a[N],S,T;
deque<int> q,emp;
bool check(double x) {
q = emp;
q.push_back(0) ;
for(int i = 1; i <= n; i++) {
while(q.front() + T < i) { q.pop_front();}
if(i - q.front() >= S && i - q.front() <= T) {
if((s[i]-s[q.front()])*1.0/(i-q.front()) >= x) return 1;
}
while(!q.empty() && s[q.back()] >= s[i]) { q.pop_back(); }
q.push_back(i);
}
return 0;
}
int main() {
scanf("%d%d%d", &n, &S, &T);
for(int i = 1; i <= n; i++) {
scanf("%d",a + i);
s[i] = s[i - 1] + a[i];
}
double l=-1e5,r=1e5;
while(l+0.0001<r) {
double mid=(l+r)/2;
if(check(mid)) l=mid;
else r=mid-1;
}
printf("%.3lf",l);
return 0;
}