斜率优化。。。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 50050 #define inf 0x7f7f7f7f7f7f7f7fLL using namespace std; long long n,s[maxn],top=0,f[maxn],g[maxn],a[maxn],b[maxn],l,r,q[maxn]; struct pnt { long long x,y; }p[maxn]; bool cmp(pnt x,pnt y) { if (x.x!=y.x) return x.x<y.x; return x.y<y.y; } double k(long long x,long long y) { if (b[x]==b[y]) return inf; return (double)(g[x]-g[y])/(b[x]-b[y]); } int main() { scanf("%lld",&n); for (long long i=1;i<=n;i++) scanf("%lld%lld",&p[i].x,&p[i].y); sort(p+1,p+n+1,cmp); for (long long i=1;i<=n;i++) { while ((p[i].y>=p[s[top]].y) && (top)) top--; s[++top]=i; } for (long long i=1;i<=top;i++) a[i]=p[s[i]].x,b[i]=p[s[i]].y; f[1]=a[1]*b[1];l=r=1;q[l]=1; for (long long i=2;i<=top;i++) { g[i]=f[i-1]; while ((r-l>0) && (k(q[r-1],q[r])<k(q[r],i))) r--; q[++r]=i; while ((r-l>0) && (k(q[l],q[l+1])>=-a[i])) l++; f[i]=g[q[l]]+a[i]*b[q[l]]; } printf("%lld\n",f[top]); return 0; }
时间: 2024-11-02 14:29:54