Link
题解看这个吧,写的很详细Link。
#include<cstdio>
#include<numeric>
#include<cstring>
using std::partial_sum;
using i64=long long;
const int N=253;const i64 inf=0x3f3f3f3f3f3f3f3fll;
int read(){int x;scanf("%d",&x);return x;}
i64 a[N],b[N],A[N],B[N],lim[N],f[N][N][2],g[N][N][2];
i64 max(i64 a,i64 b){return a>b? a:b;}
void min(i64&a,i64 b){a>b?a=b:0;}
int main()
{
int n=read(),t=read(),K=read();
for(int i=1;i<=n;++i) a[i]=read(),b[i]=read(),lim[i]=read();
++n,a[n]=b[n]=2e9,lim[n]=inf,partial_sum(a+1,a+n+1,A+1),partial_sum(b+1,b+n+1,B+1);
memset(f,63,sizeof f),memset(g,63,sizeof g),memset(f[0],0,sizeof f[0]),memset(g[0],0,sizeof g[0]);
for(int i=1;i<=n;++i)
for(int j=0;j<=t;++j)
for(int k=0;k<2;++k)
{
i64 c1,c2,s1,s2;
if(k*a[i]+j*b[i]<=lim[i]&&f[i-1][j][k]<inf)
{
min(f[i][j][k],f[i-1][j][k]),c1=(k*A[i-1]+j*B[i-1]+K-1)/K;
if(c1*K<=k*A[i]+j*B[i]) min(g[i][j][k],c1);
}
for(int l=0;l<j;++l)
if((c1=g[i][l][k])<inf)
{
s1=k*A[i]+l*B[i]-K*c1,s2=(max(0,s1+(j-l)*b[i]-lim[i])+K-1)/K;
if(s2*K<=s1&&f[i-1][j-l][0]<inf)
{
min(f[i][j][k],c1+s2+f[i-1][j-l][0]),c2=((j-l)*B[i-1]+K-1)/K;
if(c2*K<=(j-l)*B[i]+(s1-s2*K)) min(g[i][j][k],c1+s2+c2);
}
}
}
printf("%lld",f[n][t][1]);
}