hdu 2993 MAX Average Problem(DP+斜率优化)

MAX Average Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4153    Accepted Submission(s): 1052


Problem Description
Consider a simple sequence which only contains positive integers as a1, a2 ... an, and a number k. Define ave(i,j) as the average value of the sub sequence ai ... aj, i<=j. Let’s calculate max(ave(i,j)), 1<=i<=j-k+1<=n.
 


 

Input
There multiple test cases in the input, each test case contains two lines.
The first line has two integers, N and k (k<=N<=10^5).
The second line has N integers, a1, a2 ... an. All numbers are ranged in [1, 2000].
 


 

Output
For every test case, output one single line contains a real number, which is mentioned in the description, accurate to 0.01.
 


 

Sample Input
   
   
   
   
10 6 6 4 2 10 3 8 5 9 4 1
 


 

Sample Output
   
   
   
   
6.50
 


 

Source
2009 Multi-University Training Contest 19 - Host by BNU
 


 

Recommend
chenrui
 


 

思路:斜率优化,第一道

 

#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);
    }
}


 

 

你可能感兴趣的:(hdu 2993 MAX Average Problem(DP+斜率优化))