• BZOJ 1597 土地购买


    斜率优化。。。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define maxn 50050
    #define inf 0x7f7f7f7f7f7f7f7fLL
    using namespace std;
    long long n,s[maxn],top=0,f[maxn],g[maxn],a[maxn],b[maxn],l,r,q[maxn];
    struct pnt
    {
        long long x,y;
    }p[maxn];
    bool cmp(pnt x,pnt y)
    {
        if (x.x!=y.x) return x.x<y.x;
        return x.y<y.y;
    }
    double k(long long x,long long y)
    {
        if (b[x]==b[y]) return inf;
        return (double)(g[x]-g[y])/(b[x]-b[y]);
    }
    int main()
    {
        scanf("%lld",&n);
        for (long long i=1;i<=n;i++) scanf("%lld%lld",&p[i].x,&p[i].y);
        sort(p+1,p+n+1,cmp);
        for (long long i=1;i<=n;i++)
        {
            while ((p[i].y>=p[s[top]].y) && (top)) top--;
            s[++top]=i;
        }
        for (long long i=1;i<=top;i++) a[i]=p[s[i]].x,b[i]=p[s[i]].y;
        f[1]=a[1]*b[1];l=r=1;q[l]=1;
        for (long long i=2;i<=top;i++)
        {
            g[i]=f[i-1];
            while ((r-l>0) && (k(q[r-1],q[r])<k(q[r],i))) r--;
            q[++r]=i;
            while ((r-l>0) && (k(q[l],q[l+1])>=-a[i])) l++;
            f[i]=g[q[l]]+a[i]*b[q[l]];
        }
        printf("%lld
    ",f[top]);
        return 0;
    }
  • 相关阅读:
    构建之法读书笔记04
    团队冲刺06
    12.23——周总
    团队冲刺05
    团队冲刺04
    用户场景分析
    团队冲刺03
    返回一个二维数组中最大联通子数组的和
    团队冲刺02
    FFT/NTT基础题总结
  • 原文地址:https://www.cnblogs.com/ziliuziliu/p/6225328.html
Copyright © 2020-2023  润新知