3203: [Sdoi2013]保护出题人|三分|凸包

Orz iwtwiioi神犇

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define ll long long
#define N 150002
using namespace std;
struct W{double x,y;}a[N],st[N];
double X[N],A[N],sum[N],ans,d;
int n,top;
W operator-(W a,W b)
{
    return (W){a.x-b.x,a.y-b.y};
}
double operator*(W a,W b)
{
    return a.x*b.y-a.y*b.x;
}
double cal(int x,int y)
{
    return (sum[y]-st[x].y)/(X[y]+(double)y*d-st[x].x);
}
void solve(int x)
{
    int l=1,r=top;
    while(r-l>5)
    {
        int lmid=l+(r-l)/3;
        int rmid=r-(r-l)/3;
        if(cal(lmid,x)>cal(rmid,x))
            r=rmid;else l=lmid;
    }
    double mx=0;
    for(int i=l;i<=r;i++)
        mx=max(mx,cal(i,x));
    ans+=mx;
}
int main()
{
    scanf("%d%lf",&n,&d);
    for(int i=1;i<=n;i++)
    {
        scanf("%lf%lf",&A[i],&X[i]);
        sum[i]=sum[i-1]+A[i];
        a[i]=(W){i*d,sum[i-1]};
    }
    for(int i=1;i<=n;i++)
    {
        while(top>1&&(a[i]-st[top-1])*(st[top]-st[top-1])>=0)top--;
        st[++top]=a[i];
        solve(i);
    }
    printf("%.0lf",ans);
    return 0;
}

你可能感兴趣的:(凸包,三分)