Official

G - 同意書 Editorial by kaage


「同意書を既に提出している人の集合」を全探索します。 人全体の番号の集合 \(U=\{x|x\in\mathbb{N},1\leq x\leq N\}\) の部分集合は \(2^N\) 個ありますが、今回は \(N\leq14\) なので十分間に合います。人の集合を全探索した上で、与えられた条件に当てはまるかを調べ、適する集合の中で最大の要素数を求めれば良いです。

#include <iostream>
#define rep(i, n) for (int i = 0; i < int(n); i++)
template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
	if (lhs < rhs) {
		lhs = rhs;
		return true;
	}
	return false;
}
int N, M, l[110], r[110], x[110];
int main() {
	std::cin >> N >> M;
	rep(i, M) {
		std::cin >> l[i] >> r[i] >> x[i];
		l[i]--;
		r[i]--;
	}
	int ans = -1;
	rep(i, (1 << N)) {
		int cnt;
		rep(j, M) {
			cnt = 0;
			for (int k = l[j]; k <= r[j]; k++) {
				if (i & (1 << k)) cnt++;
			}
			if (cnt != x[j]) goto end;
		}
		cnt = 0;
		rep(j, N) if (i & (1 << j)) cnt++;
		chmax(ans, cnt);
	end:;
	}
	std::cout << ans << std::endl;
	return 0;
}

posted:
last update: