【题目链接】
【前置技能】
- 斜率优化DP
【题解】
- 首先要发现如果一个矩形的长和宽都小于等于另一个矩形,那么在选择较大的矩形时一定可以顺便把较小的矩形选掉。一开始我们就以 x [ i ] x[i] x[i]为关键字排序,用一个单调栈来筛选一下矩形。那么最后剩下的矩形一定是 x x x递增, y y y递减。
- 设计状态 f [ i ] f[i] f[i]表示已经购买了 1 1 1 ~ i i i号土地的最少花费。答案就是 f [ n ] f[n] f[n]。
- 显然,购买土地的时候一定是要同时购买连续的一段土地。转移方程 f [ i ] = m i n { f [ j ] + x [ i ] ∗ y [ j + 1 ] } f[i] = min\{f[j] + x[i]*y[j + 1]\} f[i]=min{f[j]+x[i]∗y[j+1]}。
- 以 ( − y [ i + 1 ] , f [ i ] ) (-y[i+1],f[i]) (−y[i+1],f[i])为坐标,维护下凸壳,用 x [ i ] x[i] x[i]切凸壳进行转移即可。
- 时间复杂度 O ( N l o g N ) O(NlogN) O(NlogN)
【代码】
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
#define MAXN 50010
using namespace std;
struct dot{LL x, y; int id;}q[MAXN];
int n, l, r;
LL f[MAXN];
struct node{int x, y;}a[MAXN];
dot operator - (dot a, dot b){
a.x -= b.x, a.y -= b.y;
return a;
}
LL operator * (dot a, dot b){
return a.x * b.y - a.y * b.x;
}
template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void read(T &x){
x = 0; int f = 1; char ch = getchar();
while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
x *= f;
}
bool cmp(node a, node b){
return a.x < b.x;
}
int main(){
read(n);
for (int i = 1; i <= n; ++i)
read(a[i].x), read(a[i].y);
sort(a + 1, a + 1 + n, cmp);
int cnt = 1;
for (int i = 2; i <= n; ++i){
while (cnt && a[i].y >= a[cnt].y) --cnt;
a[++cnt] = a[i];
}
n = cnt;
f[0] = 0;
q[l = r = 0] = (dot){-a[1].y, f[0], 0};
for (int i = 1; i <= n; ++i){
while (l < r && 1ll * a[i].x * (q[l + 1].x - q[l].x) >= q[l + 1].y - q[l].y) ++l;
int cur = q[l].id;
f[i] = f[cur] + 1ll * a[cur + 1].y * a[i].x;
dot tmp = (dot){-a[i + 1].y, f[i], i};
while (l < r && (tmp - q[r]) * (q[r] - q[r - 1]) >= 0) --r;
q[++r] = tmp;
}
printf("%lld\n", f[n]);
return 0;
}