[Bzoj1597][Usaco2008 Mar]土地购买

#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
struct data{
    int x,y;
}a[50005];
int q[50005],l,r,tot,n;
long long x[50005],y[50005],f[50005];
inline bool cmp(data a,data b){
    return a.x==b.x?a.y<b.y:a.x<b.x;
}
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return f*x;
}
inline double calc(int k,int j){
    return (double)(f[j]-f[k])/(y[k+1]-y[j+1]);
}
int main(){
    n=read();
    for(int i=1;i<=n;i++){
        a[i].x=read();a[i].y=read();
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++){
        while(tot&&a[i].y>=y[tot])tot--;
        x[++tot]=a[i].x;y[tot]=a[i].y;
    }
    for(int i=1;i<=tot;i++){
        while(l<r&&calc(q[l],q[l+1])<x[i])l++;
        int t=q[l];f[i]=f[t]+y[t+1]*x[i];
        while(l<r&&calc(q[r],i)<calc(q[r-1],q[r]))r--;
        q[++r]=i;
    }
    printf("%lld",f[tot]);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值