# bzoj1597 [Usaco2008 Mar]土地购买——斜率优化DP

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const maxn=50005;
int n,cnt,q[maxn],h,t;
ll f[maxn],x[maxn],y[maxn];
struct N{ll a,b;}p[maxn];
ll rd()
{
ll ret=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
//bool cmp(N x,N y){return x.a==y.a?x.b>y.b:x.a<y.a;}
bool cmp(N x,N y){return x.a==y.a?x.b>y.b:x.a<y.a;}
double slp(int a,int b){return (double)(f[b]-f[a])/(double)(y[a+1]-y[b+1]);}
int main()
{
n=rd();
for(int i=1;i<=n;i++)p[i].a=rd(),p[i].b=rd();
sort(p+1,p+n+1,cmp);
for(int i=1;i<=n;i++)
{
//        if(p[i].a==p[i-1].a)continue;
//        x[++cnt]=p[i].a; y[cnt]=p[i].b;
while (cnt &&  p[i].b>=y[cnt]) cnt--;
x[++cnt]=p[i].a; y[cnt]=p[i].b;
}
//    h=1;t=1;//
for(int i=1;i<=cnt;i++)
{
while(h<t&&slp(q[h],q[h+1])<x[i])h++;//<
f[i]=f[q[h]]+x[i]*y[q[h]+1];
while(h<t&&slp(q[t-1],q[t])>slp(q[t],i))t--;
q[++t]=i;
}
printf("%lld",f[cnt]);
return 0;
}

posted @ 2018-06-19 13:30  Zinn  阅读(111)  评论(0编辑  收藏  举报