题目链接:
https://www.luogu.org/problem/show?pid=1419
题意:
题解:
http://www.cnblogs.com/lidaxin/p/4917391.html
二分+单调队列。
首先二分最大平均值x。
那么问题就转化为:是否存在一个区间的的平均值大于x。这个问题可以类比于UVa11090 Going in Cycle!!【见上篇博客】,我们将a全部减去x,问题进一步转化为判断是否存在一个长度在s..t范围内的区间它的和为正,如果有说明还有更大的平均值。
如何判断?单调队列。
令sum表示a-x的前缀和。则上述条件可以变化为sumi-sumj>=0,对于i我们需要找到指定区间内的最小sumj。
单调队列维护序号在i-t到i-s的区间,保持sum的递增序,求区间最大值,判断与0的关系即可。
想了半天! 蒟蒻想说: 太难了啊!!!
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 1e5+10;
const double eps = 1e-6;
int n,s,t;
int a[maxn];
double sum[maxn];
// struct node{
// double x;
// int y;
// }v[maxn];
// bool check(double x){
// sum[0]=0;
// MS(v);
// for(int i=1; i<=n; i++) sum[i] = sum[i-1] + a[i] - x;
// int head=1,tail=0;
// for(int i=s; i<=n; i++){
// while(head<=tail && sum[i-s]<=v[tail].x) tail--;
// tail++; v[tail].x=sum[i-s],v[tail].y=i-s;
// while(head<=tail && i-t>v[head].y) head++;
// if(head<=tail && sum[i]-v[head].x >= 0) return true;
// }
// return false;
// }
int q[maxn],front,rear;
bool check(double x) {
sum[0]=0;
for(int i=1;i<=n;i++) sum[i] = sum[i-1]+a[i]-x;
front=1; rear=0; //初始化单调队列为空
for (int i = s; i <= n; i++) {
if (i >= s) { //足够s个 //front为在i-t..i-s区间内的最小值
while (rear >= front && sum[i - s] < sum[q[rear]]) rear--;
q[++rear] = i - s; //入队区间起点-1
}
if (front <= rear && q[front] < i - t) front++; //维护区间i-t
if (front <= rear && sum[i] - sum[q[front]] >= 0) return true; //sum[i]-min(sum[i-t~i-s]长度在(s~t)之间) 有大于0的区间和说明最大平均值还可以更大
}
return false;
}
int main(){
double L=1e5,R=-1e5;
scanf("%d%d%d",&n,&s,&t);
for(int i=1; i<=n; i++){
scanf("%d",&a[i]);
L = min(L,a[i]*1.0);
R = max(R,a[i]*1.0);
}
double ans = 0;
while((R-L)>eps){
double mid = (R+L)/2;
if(check(mid)) ans=mid,L=mid+eps;
else R=mid-eps;
}
printf("%.3lf\n",ans);
return 0;
}