zoukankan      html  css  js  c++  java
  • BZOJ1597_土地购买_KEY

    题目传送门

    一道斜率优化的题目。

    但暴力方程很关键。

    我们先将x作为关键字Sort一遍,再将y处理成单调递减,即把无用的土地去除。

    f[i]=f[j]+a[i]*b[j+1]
    -a[i]*b[j+1]+f[i]=f[j]
    k x + b = y

    然后单调队列维护凸包做斜率优化就好了。

    code:

    /**************************************************************
        Problem: 1597
        User: yekehe
        Language: C++
        Result: Accepted
        Time:104 ms
        Memory:3268 kb
    ****************************************************************/
     
    #include <cstdio>
    #include <algorithm>
    using namespace std;
     
    char tc()
    {
        static char tr[100000],*A=tr,*B=tr;
        return A==B&&(B=(A=tr)+fread(tr,1,100000,stdin),A==B)?EOF:*A++;
    }
     
    int read()
    {
        char c;while(c=tc(),c<'0'||c>'9');
        int x=c-'0';while(c=tc(),c>='0'&&c<='9')x=x*10+c-'0';
        return x;
    }
     
    int N,tot;
    struct node{long long x,y;}a[50005],c[50005];
    inline int cmp(node x,node y){return x.x<y.x;}
     
    long long l[50005],h,t,f[50005];
     
    double X(int x){return c[x+1].y;}
    double Y(int y){return f[y];}
    double get(int x,int y){return (Y(y)-Y(x))/(X(y)-X(x));}
     
    int main()
    {
        N=read();
        register int i;
            for(i=1;i<=N;i++)a[i].x=read(),a[i].y=read();
        sort(a+1,a+N+1,cmp);
            for(i=1;i<=N;i++){
                while(tot&&a[i].y>=c[tot].y)tot--;
                c[++tot]=a[i];
            }
        h=t=0;
            for(i=1;i<=tot;i++){
                while(h<t&&-c[i].x<get(l[h],l[h+1]))h++;
                int j=l[h];f[i]=f[j]+c[i].x*c[j+1].y;
                while(h<t&&get(l[t-1],l[t])<get(l[t],i))t--;
                l[++t]=i;
            }
        printf("%lld",f[tot]);
        return 0;
    }
  • 相关阅读:
    小程序(四):模板
    小程序(三):授权登录
    小程序(二)
    小程序(一)
    从零开始学习微信小程序
    flex 弹性布局
    如何使用docker进行shadsocks环境开发配置
    eclipse 设置注释模板
    idea 2019.1.3最新注册码
    centos7安装rabbitmq简单方式
  • 原文地址:https://www.cnblogs.com/Cptraser/p/8559833.html
Copyright © 2011-2022 走看看