AtCoder - abc383_f
Chthohepta · · 个人记录
思路
先按颜色排个序,相同颜色一定是一个连续段,在每个不同颜色的连续段 dp 即可。
对于每个连续段内,设
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 503, M = 5e4 + 3;
int n, x, k;
struct w{
int p, u, c;
}a[N];
ll dp[N][M][2], ans;
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> x >> k;
for(int i = 1; i <= n; i ++){
cin >> a[i].p >> a[i].u >> a[i].c;
}
sort(a + 1, a + 1 + n, [] (const w &x, const w &y) { return x.c < y.c; });
for(int i = 0; i <= n; i ++){
for(int j = 0; j <= x; j ++){
dp[i][j][0] = dp[i][j][1] = -1e18;
}
}
dp[0][x][0] = 0;
for(int i = 1; i <= n; i ++){
for(int j = 0; j <= x; j ++){
if(a[i].c == a[i - 1].c){
dp[i][j][0] = dp[i - 1][j][0];
dp[i][j][1] = dp[i - 1][j][1];
if(j + a[i].p <= x) dp[i][j][1] = max({dp[i - 1][j][1], dp[i - 1][j + a[i].p][1] + a[i].u, dp[i - 1][j + a[i].p][0] + a[i].u + k});
}
else{
dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j][1]);
if(j + a[i].p <= x) dp[i][j][1] = max({dp[i - 1][j + a[i].p][1], dp[i - 1][j + a[i].p][0]}) + a[i].u + k;
}
}
}
for(int i = 0; i <= x; i ++){
ans = max({ans, dp[n][i][0], dp[n][i][1]});
}
cout << ans;
return 0;
}