BZOJ1597 & 洛谷2900：[USACO2008 MAR]Land Acquisition 土地购买——题解

http://www.lydsy.com/JudgeOnline/problem.php?id=1597

https://www.luogu.org/problemnew/show/P2900

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=50010;
inline int read(){
int X=0,w=1;char ch=0;
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')X=(X<<1)+(X<<3)+ch-'0',ch=getchar();
return X*w;
}
struct buy{
ll l,w;
}a[N],b[N];
bool cmp(buy a,buy b){
return a.l>b.l||(a.l==b.l&&a.w>b.w);
}
int n,cnt,l,r,maxw;
ll f[N],q[N];
inline double suan(int j,int k){
return 1.0*(f[j]-f[k])/(b[k+1].l-b[j+1].l);
}
int main(){
n=read();
for(int i=1;i<=n;i++)
a[i].l=read(),a[i].w=read();
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
if(a[i].w>maxw){
maxw=a[i].w;
b[++cnt]=a[i];
}
}
for(int i=1;i<=cnt;i++){
while(l<r&&suan(q[l],q[l+1])<(double)b[i].w)l++;
f[i]=f[q[l]]+b[q[l]+1].l*b[i].w;
while(l<r&&suan(q[r],i)<suan(q[r-1],q[r]))r--;
q[++r]=i;
}
printf("%lld\n",f[cnt]);
return 0;
}

+++++++++++++++++++++++++++++++++++++++++++

+本文作者：luyouqi233。 　　　　　　　　　　　　　　+

+欢迎访问我的博客：http://www.cnblogs.com/luyouqi233/+

+++++++++++++++++++++++++++++++++++++++++++

posted @ 2018-01-31 17:33  luyouqi233  阅读(227)  评论(0编辑  收藏  举报