dp &单调队列优化
这个题 : k<=i, 决策点k 可以等于i ,所以在 i 入队后递推
#include<iostream> #include<algorithm> #include<cstring> using namespace std; const int N =503,M =1e4+2; #define int long long int n,m,E,X[N],F[N],C[N],f[N][M]; int q[N],hh,tt; struct T{ int C,F,x; }a[N]; int cmp(T &a,T &b){ return a.x<b.x; } int func(int i,int k){ return f[i - 1][k]-C[i]*k + k*k*(X[i]-X[i-1]); } signed main(){ cin>>m>>E>>n; int i,j; for(i=1;i<=n;i++) cin>>a[i].x>>a[i].F>>a[i].C; sort(a+1,a+1+n,cmp); for(i=1;i<=n;i++) X[i]=a[i].x,F[i]=a[i].F,C[i]=a[i].C; memset(f,0x3f,sizeof f); for(i=0;i<=n;i++) f[i][0]=0; for(i=1;i<=n;i++){ hh=1,tt=0; q[++tt]=0; for(j=1;j<=m;j++){ while(hh<=tt&&q[hh]<j-min(F[i],j)) hh++; while(hh<=tt&&func(i,j)<=func(i,q[tt])) tt--; q[++tt]=j ; int k=q[hh]; f[i][j]= func(i,k)+C[i]*j; } } int ans=f[n][m]+ m*m*(E-X[n]); cout<<ans; }
标签:Feed,int,P4544,long,include,Buying From: https://www.cnblogs.com/towboa/p/17282818.html