# BZOJ1597_土地购买_KEY

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;
}

{
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()
{
register int i;
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;
}

posted @ 2018-03-13 18:06  Cptraser  阅读(122)  评论(0编辑  收藏  举报