斜率优化实现初步(3) [BZOJ][1597][Usaco2008 Mar]土地购买

#include<bits/stdc++.h>
using namespace std;

#define int long long
const int MAXN=5e4+23333;

struct Rectangle{
    int a,b;
    bool operator < (const Rectangle&x) const {
        return x.a!=a ? x.a>a:x.b>b;
    }
}dat[MAXN];

int n,cnt;
int a[MAXN],b[MAXN];
int head=1,tail=1,q[MAXN];
int f[MAXN];

int read(){
    int x=0,f=1;char ch=getchar();
    while (!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar();}
    while (isdigit(ch)) x=x*10+ch-48,ch=getchar();
    return x*f;
}

int slope(int j,int k){
    return (f[j]-f[k])/(b[k+1]-b[j+1]);
}

signed main(){
    n=read();
    for (int i=1;i<=n;i++)
        dat[i].a=read(),dat[i].b=read();
    sort(dat+1,dat+n+1);
    for (int i=1;i<=n;i++){
        while (cnt!=0&&dat[i].b>=b[cnt]) cnt--;
        a[++cnt]=dat[i].a,b[cnt]=dat[i].b;
    }
    for (int i=1;i<=cnt;i++){
        while (head<tail&&slope(q[head+1],q[head])<a[i])
            head++;
        f[i]=f[q[head]]+a[i]*b[q[head]+1];
        while (head<tail&&slope(i,q[tail])<slope(q[tail-1],q[tail]))
            tail--;
        q[++tail]=i;
    }
    printf("%lld\n",f[cnt]);
    return 0;
}

 

posted @ 2018-08-25 12:25  QingCai_DCF  阅读(173)  评论(0编辑  收藏  举报