我推出来式子为 bj−bkfj−1−fk−1>−ai
于是我维护了个上凸包。
我看了很久都没发现问题,麻烦大佬帮我看看。
#include<bits/stdc++.h>
#define int long long
#define N 70004
using namespace std;
int n;
int f[N];
struct node{
int a, b;
}p[N], c[N];
int S[N], tot;
bool cmp(node x, node y) {
if(x.a == y.a) return x.b < y.b;
return x.a < y.a;
}
double slope(int k, int j) {
return 1.000 * (f[j - 1] - f[k - 1]) / (c[j].b - c[k].b);
}
int Q[N], head = 1, tail; //[head, tail]
signed main() {
scanf("%lld", &n);
for(int i = 1; i <= n; i++) scanf("%lld %lld", &p[i].a, &p[i].b);
sort(p + 1, p + n + 1, cmp);
for(int i = 1; i <= n; i++) {
while(tot >= 1 && p[i].b >= p[S[tot]].b) tot--;
S[++tot] = i;
}
for(int i = 1; i <= tot; i++) {
c[i].a = p[S[i]].a;
c[i].b = p[S[i]].b;
}
n = tot;
f[1] = c[1].a * c[1].b;
Q[++tail] = 1;
for(int i = 2; i <= n; i++) {
while(head < tail && slope(head, head + 1) > -c[i].a) head++;
if(head <= tail) {
int j = Q[head];
f[i] = min(f[i - 1] + c[i].a * c[i].b, f[j - 1] + c[i].a * c[j].b);
}
while(head < tail && slope(tail, i) > slope(tail - 1, tail)) tail--;
Q[++tail] = i;
}
printf("%lld", f[n]);
return 0;
}