题意:求出长度大于k子序列使得其各个元素之和的平均数最大,并输出最大平均值
代码:
#include<stdio.h> #include<iostream> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int MAXN=100010; int sum[MAXN]; int q[MAXN]; int top; long long cross(int a,int b,int c) { long long x1=b-a; long long y1=sum[b]-sum[a]; long long x2=c-b; long long y2=sum[c]-sum[b]; return x1*y2-y1*x2; } int bsearch(int l,int r,int i) { while(l<r) { int mid=(l+r)>>1; if(cross(q[mid],q[mid+1],i)<0)r=mid; else l=mid+1; } return l; } int input() { char ch=‘ ‘; while(ch<‘0‘||ch>‘9‘)ch=getchar(); int x=0; while(ch<=‘9‘&&ch>=‘0‘)x=x*10+ch-‘0‘,ch=getchar(); return x; } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int n,k; while(scanf("%d%d",&n,&k)!=EOF) { top=0; sum[0]=0; for(int i=1;i<=n;i++) { sum[i]=input(); sum[i]+=sum[i-1]; } double ans=0; q[top++]=0; for(int i=k;i<=n;i++) { int j=i-k; while(top>1&&cross(q[top-2],q[top-1],j)<0)top--; q[top++]=j; int temp=bsearch(0,top-1,i); double f=((double)(sum[i]-sum[q[temp]]))/(i-q[temp]); if(f>ans)ans=f; } printf("%.2lf\n",ans); } return 0; }
时间: 2024-11-06 03:32:30