如图,看完题目知道是dp,还是没想出来怎么写(因为条件较多)。
先贴题解,留待复习。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 110;
const int inf = 1e9;
ll dp[maxn][maxn];
ll a[maxn], b[maxn], c[maxn];
int n, m;
int T;
int main()
{
scanf("%d", &T);
while(T--)
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
cin >> a[i] >> b[i] >> c[i];
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= i; j++)
dp[i][j] = -inf;
}
dp[0][0] = m;
c[0]= 0;
for(int i = 1; i <= n; i++)
{
for(int j = 0; j <= i; j++)
{
ll go = -inf;
if(j != 0 && min(c[i - 1] + dp[i - 1][j - 1], b[i]) > a[i])
{
go = min(c[i - 1] + dp[i - 1][j - 1], b[i]) - a[i];
}
dp[i][j] = max(dp[i - 1][j] + c[i - 1], go);
}
}
for(int i = n; i >= 1; i--)
{
if(dp[n][i] > 0)
{
cout << i << endl;
break;
}
}
}
return 0;
}