分析
https://blog.csdn.net/JacaJava/article/details/78998056 不太会用博客园的数学公式,就把教我的大佬的博客贴一下8
#include <iostream> #include <cstdio> using namespace std; typedef long long ll; const int N=1e5+10; int n,m,q[N]; ll f[N],t[N],p[N]; int tail; double xl(int i,int j) { return (double)(t[i]-t[j])/(t[i-1]-t[j-1]); } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%lld",&t[i]),t[i]+=t[i-1]; for (int i=1;i<=m;i++) scanf("%lld",&f[i]); q[++tail]=1; for (int i=1;i<=n;i++) { while (tail>1&&xl(i,q[tail])>xl(q[tail],q[tail-1])) tail--; q[++tail]=i; } for (int i=2;i<=m;i++) { int l=1,r=tail,mid; double o=(double)f[i]/f[i-1]; while (l<r) { mid=l+r>>1; if (o<xl(q[mid+1],q[mid])) l=mid+1; else r=mid; } p[i]=p[i-1]+f[i-1]*t[q[l]]-f[i]*t[q[l]-1]; } printf("%lld",p[m]+f[m]*t[n]); }