poj2018 Best Cow Fences

poj2018 Best Cow Fences_第1张图片 poj2018 Best Cow Fences_第2张图片 poj2018 Best Cow Fences_第3张图片 poj2018 Best Cow Fences_第4张图片 poj2018 Best Cow Fences_第5张图片 poj2018 Best Cow Fences_第6张图片
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXSIZE 100500
#define sf scanf
#define pf printf
using namespace std;
int N,F;
int sum[MAXSIZE],t[MAXSIZE];
int stack[MAXSIZE],top,back;
long long Xmult(int i,int j,int k)
{
    return (long long )(i-j)*(sum[k]-sum[j])-(long long)(k-j)*(sum[i]-sum[j]);
}
void in_stack(int i)
{
    while(top>=2&&Xmult(i,stack[top-1],stack[top-2])<=0) top--;
    stack[top++]=i;
}
int main()
{
    while(~sf("%d%d",&N,&F))
    {
        sum[0]=0;
        for(int i=1; i<=N; i++)
        {
            sf("%d",&t[i]);
            sum[i]=sum[i-1]+t[i];
        }
        int i,j;
        top=back=0;
        long long ant=-1;
        for(i=F,j=0; i<=N; i++,j++)
        {
            in_stack(j);
            if(back>top-1)
            {
                long long temp;
                back=top-1;
                temp=(long long)(sum[i]-sum[stack[top-1]])*1000/(i-stack[top-1]);
                if(temp>ant)
                    ant=temp;
                    continue ;
            }
            while(back<top-1)
            {
                int x=stack[back];
                int y=stack[back+1];
                if((long long)(sum[i]-sum[y])*1000/(i-y)>=(long long)(sum[i]-sum[x])*1000/(i-x))
                back++;
                else break;
            }

            long long temp;
                temp=(long long)(sum[i]-sum[stack[back]])*1000/(i-stack[back]);
                if(temp>ant)
                    ant=temp;

            }
            pf("%lld\n",ant);
        }
        }



 
 

你可能感兴趣的:(ant)