跳到正文

题解

题解:AT_abc375_e [ABC375E] 3 Team Division

hwq202314约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年10月12日
收录
2026年10月19日
更新
2026年11月15日
篇幅
约 3 分钟

版本

  1. v1当前

Link

评价也是糖题。

一眼值域 DP,设 fx,y,zf_{x,y,z} 表示三个团队的力量和分别为 x,y,zx,y,z 所需的最小代价。由于数据范围极小,我们直接暴力转移即可,应该很好想,注意转移时判断一下代价和是否超过 m2\frac{m}{2},其中 m=∑bim = \sum b_i,我就因此 RE 吃了一发罚时。

AC 代码
C++
// 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;
}
原文