# BZOJ 1597 土地购买(斜率优化DP)

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi 3.1415926535
# define eps 1e-9
# define MOD 100000007
# define INF 1000000000
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int res=0, flag=0;
char ch;
if((ch=getchar())=='-') flag=1;
else if(ch>='0'&&ch<='9') res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')  res=res*10+(ch-'0');
return flag?-res:res;
}
void Out(int a) {
if(a<0) {putchar('-'); a=-a;}
if(a>=10) Out(a/10);
putchar(a%10+'0');
}
const int N=50005;
//Code begin...

struct Node{int x, y;}node[N];
LL dp[N];
int que[N], head=-1, tail;

bool comp(Node a, Node b){
if (a.x==b.x) return a.y>b.y;
return a.x>b.x;
}
bool check(int x, int y, int z){return dp[x]-dp[y]<=(LL)-node[z].y*(node[x+1].x-node[y+1].x);}
bool sol(int x, int y, int z){
return (dp[x]-dp[y])*(node[y+1].x-node[z+1].x)>=(dp[y]-dp[z])*(node[x+1].x-node[y+1].x);
}
int main ()
{
int n;
scanf("%d",&n);
FOR(i,1,n) scanf("%d%d",&node[i].x,&node[i].y);
sort(node+1,node+n+1,comp);
int pos=1;
FOR(i,2,n) if (node[pos].y<node[i].y) node[++pos].x=node[i].x, node[pos].y=node[i].y;
que[++head]=0;
FOR(i,1,pos) {
while (head>tail&&check(que[tail+1],que[tail],i)) ++tail;
int v=que[tail];
dp[i]=(LL)node[i].y*node[v+1].x+dp[v];
while (head>tail&&sol(i,que[head],que[head-1])) --head;
que[++head]=i;
}
printf("%lld\n",dp[pos]);
return 0;
}
View Code

posted @ 2017-03-21 16:18  free-loop  阅读(118)  评论(0编辑  收藏  举报