hdu 2993 MAX Average Problem 动态规划 斜率优化

做法:构造凸包即可。然后HDU的时间卡的很死,手动输入,能用存储位数少的数据类型就用,计算机组成了刚说过,呵呵。。

#include<cstdio>
#define LL __int64
const int LMT=100005;
int deq[LMT],rear,top;
int sum[LMT];
int get(void)
{
    char c=getchar();
    int res=0;
    while(c<'0'||c>'9')c=getchar();
    while(c>='0'&&c<='9')
    {
        res*=10;
        res+=c-'0';
        c=getchar();
    }
    return res;
}
int main(void)
{
    int i,j,n,k,x;
    LL help=1;
    while(~scanf("%d%d",&n,&k))
    {
        double ans=0.0;
        for(i=1;i<=n;i++)
        {
            x=get();
            sum[i]=sum[i-1]+x;
        }
        rear=top=0;
        deq[top++]=0;
        for(i=k;i<=n;i++)
        {
            while(rear<top-1)
            {
                if(help*(sum[i]-sum[deq[rear]])*(i-deq[rear+1])<=help*(sum[i]-sum[deq[rear+1]])*(i-deq[rear]))
                    rear++;
                else break;
            }
            ans=ans>(sum[i]-sum[deq[rear]])*1.0/(i-deq[rear])?ans:(sum[i]-sum[deq[rear]])*1.0/(i-deq[rear]);
            j=i-k+1;
            while(rear<top-1)
            {
                if(help*(sum[j]-sum[deq[top-1]])*(deq[top-1]-deq[top-2])<
                    help*(sum[deq[top-1]]-sum[deq[top-2]])*(j-deq[top-1]))top--;
                else break;
            }
            deq[top++]=j;
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}


 

你可能感兴趣的:(hdu 2993 MAX Average Problem 动态规划 斜率优化)