# [BZOJ] 1597: [Usaco2008 Mar]土地购买

$f[i]$表示前$i$块土地，第$i$块必选的最小代价

$f[i]=\min_{j=1}^{i-1}\{f[j]+cost[j+1][i]\}$

$cost[i][j]=h[i]*\max_{k=i}^j\{w[k]\}$

$f[i]=\min_{j=1}^{i-1}\{f[j]+h[j+1]\times w[i]\}$

$cost[i][j]=w[i]\times h[j+1]$

\begin{align} cost[i][j]+cost[i+1][j+1]=w[i]\times h[j+1]+w[i+1]\times h[j+2]\\ cost[i+1][j]+cost[i][j+1]=w[i+1]\times h[j+1]+w[i]\times h[j+2] \end{align}

(1)式减(2)式得

$LHS=cost[i][j]+cost[i+1][j+1]-cost[i+1][j]-cost[i][j+1]$

\begin{align*} RHS&=w[i]\times (h[j+1]-h[j+2])+w[i+1]\times(h[j+2]-h[j+1])\\ &=(h[j+1]-h[j+2])\times (w[i]-w[i+1]) \end{align*}

\begin{align*} h[j+1]-h[j+2]>0\\ w[i]-w[i+1]<0 \end{align*}

$RHS<0$

$cost[i][j]+cost[i+1][j+1]<cost[i][j+1]+cost[i+1][j]$

（不知道为什么甚至用了deque，跑得比大部分$O(n)$的斜率优化还快）

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<bitset>
#include<deque>
using namespace std;

inline int rd(){
int ret=0,f=1;char c;
while(c=getchar(),!isdigit(c))f=c=='-'?-1:1;
while(isdigit(c))ret=ret*10+c-'0',c=getchar();
return ret*f;
}

const int MAXN = 50005;

inline void upmax(int &x,int y){x=max(x,y);}
inline void upmin(int &x,int y){x=min(x,y);}

int n;
typedef long long ll;

struct Node{
int x,y;
bool operator <(const Node &rhs)const{
return x==rhs.x?y>rhs.y:x>rhs.x;
}
}tmp[MAXN],node[MAXN];
int tot;

struct Que{
int l,r,p;
Que(int _p,int _l,int _r){l=_l;r=_r;p=_p;}
};

deque<Que> Q;
ll f[MAXN];

ll calc(int p,int x){
return f[p]+1ll*node[p+1].x*node[x].y;
}

int fnd(int x){
int l=Q.back().l,r=Q.back().r,p=Q.back().p,ret;
while(l<r){
int mid=(l+r)>>1;
if(calc(x,mid)<=calc(p,mid))r=mid;
else ret=mid,l=mid+1;
}
return ret+1;
}

signed main(){
memset(f,0x7f,sizeof(f));
f[0]=0;
n=rd();
for(int i=1;i<=n;i++)tmp[i].x=rd(),tmp[i].y=rd();
sort(tmp+1,tmp+1+n);
for(int i=1;i<=n;i++){
if(tot&&node[tot].y>=tmp[i].y)continue;
node[++tot]=tmp[i];
}
Q.push_back(Que(0,1,tot));
for(int i=1;i<=tot;i++){
while(!Q.empty()&&Q.front().r<i)Q.pop_front();
f[i]=calc(Q.front().p,i);//f[i]=f[p]+cost[p+1][i]
if(calc(i,tot)>calc(Q.back().p,tot))continue;
while(!Q.empty()&&calc(i,Q.back().l)<calc(Q.back().p,Q.back().l))Q.pop_back();
if(Q.empty()){Q.push_back(Que(i,i+1,tot));continue;}
int pos=fnd(i);
Q.back().r=pos-1;Q.push_back(Que(i,pos,tot));
}
cout<<f[tot];
return 0;
}


posted @ 2018-10-18 16:12  GhostCai  阅读(70)  评论(0编辑  收藏  举报