Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4153 Accepted Submission(s): 1052
10 6 6 4 2 10 3 8 5 9 4 1
6.50
思路:斜率优化,第一道
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int mm=100009; long long sum[mm]; typedef pair<int,int> P; P poin[mm]; int n,m,pos; bool cross(P a,P b,P c)///上凸 { long long x,y,xx,yy; x=b.first-a.first;y=b.second-a.second; xx=c.first-b.first;yy=c.second-b.second; return y*xx>yy*x; } int bser(int l,int r,P&now) { int mid; while(l<r) { mid=(l+r)/2; if(cross(poin[mid],poin[mid+1],now))r=mid; else l=mid+1; } return l; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { sum[0]=0; for(int i=1;i<=n;i++) { scanf("%d",&sum[i]);sum[i]+=sum[i-1]; } pos=0; float ans=0.0; for(int i=m;i<=n;i++) { P r(i-m,sum[i-m]),now(i,sum[i]); while(pos>1&&cross(poin[pos-1],poin[pos],r))pos--; poin[++pos]=r; int opt=bser(1,pos,now);///二分找最低点 ///int opt=pos; double mid=(double)(sum[i]-poin[opt].second)/double(i-poin[opt].first);///最大斜率 if(mid>ans)ans=mid; } printf("%.2f\n",ans); } }