题解:AT_abc375_e [ABC375E] 3 Team Division
Link
评价也是糖题。
一眼值域 DP,设
::::success[AC 代码]
// Problem: E - 3 Team Division
// Contest: AtCoder - Panasonic Programming Contest 2024(AtCoder Beginner Contest 375)
// URL: https://atcoder.jp/contests/abc375/tasks/abc375_e
// Memory Limit: 1024 MB
// Time Limit: 4000 ms
#include <iostream>
#include <cstring>
#include <algorithm>
namespace Qwarth {
const int N = 107;
const int M = 507;
int n, m;
int a[N], b[N];
int f[M][M][M];
void main() {
std::cin >> n;
for (int i = 1; i <= n; ++i) {
std::cin >> a[i] >> b[i];
m += b[i];
}
if (m % 3) {
puts("-1");
return;
}
m /= 3;
memset(f, 0x3f, sizeof(f));
f[0][0][0] = 0;
int s = 0;
for (int i = 1; i <= n; ++i) {
for (int x = 0; x <= s && x <= m; ++x) {
for (int y = 0; y <= s - x && y <= m; ++y) {
int z = s - x - y;
if (z > m) {
continue;
}
if (x + b[i] <= m) {
f[x + b[i]][y][z] = std::min({ f[x + b[i]][y][z], f[x][y][z] + (a[i] != 1) });
}
if (y + b[i] <= m) {
f[x][y + b[i]][z] = std::min({ f[x][y + b[i]][z], f[x][y][z] + (a[i] != 2) });
}
if (z + b[i] <= m) {
f[x][y][z + b[i]] = std::min({ f[x][y][z + b[i]], f[x][y][z] + (a[i] != 3) });
}
}
}
s += b[i];
}
if (f[m][m][m] > 1000000000) {
puts("-1");
} else {
std::cout << f[m][m][m] << '\n';
}
}
} // namespace Qwarth
int main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int _T = 1;
// std::cin >> _T;
while (_T--) {
Qwarth::main();
}
return 0;
}
::::