上次做斜率优化的题目都是好几个月以前的事情了……今天来复习一波。
对于这题,我们可以先按长度从小到大排序,然后考虑土地之间包含的情况,可以发现小的土地对于这题没有用处,直接删去即可。
删去所有被包含的土地后可以发现宽度已经从大到小排序了。这个非常显然,考虑i<j,h[i]<h[j]&&w[i]<w[j],那么i就应该在上一步被删掉了。
然后我们可以得出一个非常显然的DP状态设置:f[i]表示前i块土地所花费的最小费用。
DP的状态转移方程也非常好想:f[i]=min{f[j-1]+h[i]*w[j]} (1<=j<=i)
斜率优化的推公式过程我就不再赘述了,最后得到(f[j-1]-f[k-1])/(w[k]-w[j])<h[i]表示j比k优。(1<=k<j)
然后就是斜率优化的套路,单调队列维护凸包,直接求出答案即可。
附上AC代码:
#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;
const int N=50010;
struct note{
long long x,y;
bool operator < (const note lyf) const {return x==lyf.x?y>lyf.y:x<lyf.x;}
}a[N],b[N];
long long n,m,f[N],dui[N],hd,tl,h[N],w[N];
inline char nc(){
static char ch[100010],*p1=ch,*p2=ch;
return p1==p2&&(p2=(p1=ch)+fread(ch,1,100010,stdin),p1==p2)?EOF:*p1++;
}
inline void read(long long &a){
static char c=nc();int f=1;
for (;!isdigit(c);c=nc()) if (c=='-') f=-1;
for (a=0;isdigit(c);a=a*10+c-'0',c=nc());
a*=f;return;
}
inline double calc(int x,int y){
return (double)(f[x]-f[y])/(w[y]-w[x]);
}
int main(void){
read(n);
for (int i=1; i<=n; ++i) read(a[i].x),read(a[i].y);
sort(a+1,a+1+n);
for (int i=1; i<=n; ++i){
while (m&&a[i].y>=b[m].y) --m;
b[++m]=a[i];
}
for (int i=1; i<=m; ++i) h[i]=b[i].x,w[i-1]=b[i].y;
dui[hd=tl=0]=0;
for (int i=1; i<=m; ++i){
while (hd<tl&&calc(dui[hd+1],dui[hd])<h[i]) ++hd;
f[i]=f[dui[hd]]+h[i]*w[dui[hd]];
while (hd<tl&&calc(i,dui[tl])<calc(dui[tl],dui[tl-1])) --tl;
dui[++tl]=i;
}
printf("%lld\n",f[m]);
return 0;
}