luoguP2365 任务安排 斜率优化 + 动态规划
Code:
#include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) #define ll long long #define x(i) (sumf[i]) #define y(i) (f[i]) #define maxn 1000000 using namespace std; int n,s,head,tail; int q[maxn]; ll sumt[maxn],sumf[maxn],f[maxn]; double slope(int i,int j) { return (double)(1.00*(y(i)-y(j)))/(double)(1.00*(x(i)-x(j)));} int main() { // setIO("input"); int i,j; scanf("%d%d",&n,&s); for(i=1;i<=n;++i) { scanf("%lld%lld",&sumt[i],&sumf[i]); sumt[i]+=sumt[i-1],sumf[i]+=sumf[i-1]; } head=tail=0; for(i=1;i<=n;++i) { while(head<tail&&slope(q[head],q[head+1])<=sumt[i]+s)++head; f[i]=y(q[head])+sumf[i]*sumt[i]+s*sumf[n]-(sumt[i]+s)*x(q[head]); while(head<tail&&slope(q[tail],i)<slope(q[tail-1],i)) --tail; q[++tail]=i; } printf("%lld\n",f[n]); return 0; }