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
 


 

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


 

 

转载于:https://www.cnblogs.com/nealgavin/archive/2013/01/28/3205981.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值