码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2018 Best Cow Fences

时间:2017-01-25 17:40:31      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:out   time   color   ++   pen   fence   ble   style   getchar   

斜率优化。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-10;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar();
    x = 0;
    while(!isdigit(c)) c = getchar();
    while(isdigit(c))
    {
        x = x * 10 + c - 0;
        c = getchar();
    }
}

int n,k;
double a[100010],s[100010];
int q[100010],f1,f2;

bool delete1(int a,int b,int c)
{
    if((s[c]-s[a])*(c-b)<(s[c]-s[b])*(c-a)) return 1;
    return 0;
}

bool delete2(int a,int b,int c)
{
    if((s[c]-s[b])*(b-a)<(s[b]-s[a])*(c-b)) return 1;
    return 0;
}

int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        for(int i=1;i<=n;i++) scanf("%lf",&a[i]);
        for(int i=1;i<=n;i++) s[i]=s[i-1]+a[i];

        f1=0; f2=0; q[f2]=0; double ans=0;
        for(int i=k;i<=n;i++)
        {
            while(1)
            {
                if(f2-f1+1<2) break;
                if(delete1(q[f1],q[f1+1],i)) f1++;
                else break;
            }

            ans=max(ans,1.0*(s[i]-s[q[f1]])/(i-q[f1]));

            while(1)
            {
                if(f2-f1+1<2) break;
                if(delete2(q[f2-1],q[f2],i)) f2--;
                else break;
            }

            f2++; q[f2]=i-k+1;

        }
        printf("%d\n",(int)(1000*ans));
    }
    return 0;
}

 

POJ 2018 Best Cow Fences

标签:out   time   color   ++   pen   fence   ble   style   getchar   

原文地址:http://www.cnblogs.com/zufezzt/p/6349756.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!