题意
给定若干长方形,可以将他们分为任意组,每一组的耗费为最长长乘以最长宽。求最小耗费。
思路
显然存在包含关系的长方形可以看成同一个长方形。
在去重之后,剩下的长方形满足长从小到大,宽从大到小。
那么有\(f[i]=min(f[j]+a[i]*b[j+1])\)
\(end\)。
代码
没调出来,乱搞了一下就A了。
#include <bits/stdc++.h>
using namespace std;
namespace StandardIO {
template<typename T> inline void read (T &x) {
x=0;T f=1;char c=getchar();
for (; c<'0'||c>'9'; c=getchar()) if (c=='-') f=-1;
for (; c>='0'&&c<='9'; c=getchar()) x=x*10+c-'0';
x*=f;
}
template<typename T> inline void write (T x) {
if (x<0) putchar('-'),x=-x;
if (x>=10) write(x/10);
putchar(x%10+'0');
}
}
using namespace StandardIO;
namespace Solve {
#define int long long
const int N=500005;
int n,cnt,head,tail;
int dp[N],queue[N];
struct node {
int a,b;
} s[N],q[N];
inline bool cmp (const node &a,const node &b) {
if (a.a!=b.a) return a.a>b.a;
return a.b<b.b;
}
inline double slope (int x,int y) {
return (double)(dp[x]-dp[y])/(double)(q[x+1].b-q[y+1].b);
}
inline void MAIN () {
read(n);
for (register int i=1; i<=n; ++i) {
read(s[i].a),read(s[i].b);
}
sort(s+1,s+n+1,cmp);
for (register int i=1; i<=n; ++i) {
if (s[i].b>q[cnt].b) {
q[++cnt]=s[i];
// cout<<q[cnt].a<<' '<<q[cnt].b<<endl;
}
}
reverse(q+1,q+cnt+1);
head=tail=1,queue[head]=0;
for (register int i=1; i<=cnt; ++i) {
while (head<tail&&slope(queue[head],queue[head+1])>=-q[i].a) ++head;
dp[i]=dp[queue[head]]+q[i].a*q[queue[head]+1].b;
while (head<tail&&slope(queue[tail-1],queue[tail])<=slope(queue[tail],i)) --tail;
queue[++tail]=i;
}
write(dp[cnt]);
}
#undef int
}
int main () {
Solve::MAIN();
}