跳到正文

题解

题解:AT_abc358_c [ABC358C] Popcorn

lkrqwq约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年6月16日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 1 分钟

版本

  1. v1当前

一道子集枚举题。

我们用 NN 位二进制数来表示高桥买了哪些摊位的爆米花,然后检查一下,如果合法,答案取二进制数 11 的个数的最小值即可。

时间复杂度 O(2N×(N+M))O(2^N \times (N+M))。

代码如下:

C++
int n, m;
string str[13];
int popcount(int x) {
	if (x == 0) return 0;
	return popcount(x >> 1) + (x & 1);
}
int main() {
	cin >> n >> m;
	FORL(i, 1, n, 1) {
		cin >> str[i];
	}
	int ans = 19;
	FORL(i, 1, (1 << n) - 1, 1) {
		string s = "";
		FORL(j, 1, m, 1) s += " ";
		FORL(j, 0, n - 1, 1) {
			if ((i >> j) & 1) {
				int now = n - j;
				FORL(k, 0, m - 1, 1) {
					if (str[now][k] == 'o') {
						s[k] = 'o';
					}
				}
			}
		}
		int cnt = 0;
		FORL(j, 0, m - 1, 1) {
			if (s[j] == 'o') ++cnt;
		}
		if (cnt == m) {
			ans = min(ans, popcount(i));
		}
	}
	write(ans);
}
原文