冰水挑戰
一開始想錯了方程。。。導致最後沒有做出來。
題解:通過題意我們就可以知道這是一類很基礎的揹包問題,第項選與不選,時刻保持體力大於零。
表示對於前個挑戰選了個的最大剩餘體力,那麼就有,如果不選,則;如果體力足夠並且選了第項,則。
代碼
#include<bits/stdc++.h>
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
typedef long long LL;
using namespace std;
const int N = 1024, inf = INT_MAX >> 1;
LL a[N],b[N],c[N],n;
LL dp[N][N];
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
ios::sync_with_stdio(false); cin.tie(0);
int T,C;
cin >> T;
while(T--) {
cin >> n >> C;
for(int i = 1; i <= n; ++i) {
cin >> a[i] >> b[i] >> c[i];
}
memset(dp, -0x3f, sizeof dp);
dp[0][0] = C;
for(int i = 1; i <= n; ++i) {
for(int j = 0; j <= i; ++j) {
LL cur = min(dp[i - 1][j - 1], b[i]);
dp[i][j] = max(dp[i - 1][j] + c[i], ((cur - a[i] > 0 )? (cur - a[i] + c[i]) : dp[i][j]));
}
}
int ans = 0;
for(int i = n; i >= 0; --i) {
if(dp[n][i] > 0) {
ans = i;
break;
}
}
cout << ans << endl;
}
return 0;
}