跳到正文

题解

P9333 [JOISC 2023 Day2] Council

TernaryTree约 6 分钟
原文
文章信息与版本

文章信息

发布
2024年4月20日
收录
2026年10月4日
更新
2026年11月10日
篇幅
约 6 分钟
获赞
6
题目
P9333

版本

  1. v1当前

很好的 sos-dp 练手题。

首先我们可以把 MM 个 0/10/1 看做二进制压在一个数里,称作“状态”。

不妨令 ii 为主席。若此时提案 jj 的选票 >⌊N2⌋\gt \left\lfloor\dfrac N2\right\rfloor,则无论怎么选副主席,jj 都能被通过。同理,若选票 <⌊N2⌋\lt \left\lfloor\dfrac N2\right\rfloor,则无论怎么选,jj 都无法通过。于是我们只关心那些选票 =⌊N2⌋=\left\lfloor\dfrac N2\right\rfloor 的提案。

假设主席为 ii 时选票为 ⌊N2⌋\left\lfloor\dfrac N2\right\rfloor 的提案状态为 sis_i。我们选 j (j≠i)j\ (j\neq i) 作为副主席,此时答案应为 (¬aj) & si(\lnot a_j)\ \&\ s_i。令 bi=¬aib_i=\lnot a_i,问题转换为找一个 jj 使得 si & bjs_i\ \& \ b_j 最大。这里等价于在 sis_i 的子集内枚举这个最大值更新,即高位前缀和。同时需要次大值,因为 i≠ji\neq j。

前缀和之前的状态应该是什么呢,也就是这个状态 ss 是不是一个 bib_i 的子集。这里高维后缀和就行,也需要存两个值,不过任意存就行了。

复杂度 Θ(NM+M2M)\Theta(NM+M2^M)。

C++
#include <bits/stdc++.h>
#define int long long
#define fs first
#define sc second
#define ls (u << 1)
#define rs (u << 1 | 1)
#define mid ((l + r) >> 1)
#define lc ls, l, mid
#define rc rs, mid + 1, r
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define per(i, r, l) for (int i = (r); i >= (l); --i)
#define gc getchar
#define pc putchar

using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;

const int maxn = 1e6 + 10;
const int maxm = 22;
const bool multidata = 0;

int read() {
	int x = 0, f = 1; char c = gc();
	while (c < '0' || c > '9') { if (c == '-') f = -f; c = gc(); }
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc();
	return x * f;
}

void write(int x) {
	if (x < 0) pc('-'), x = -x;
	if (x < 10) return void (pc(x + '0'));
	write(x / 10), pc(x % 10 + '0');
}

int n, m, msk, tmp;
int a[maxn], b[maxn], c[1 << maxm];
int p[1 << maxm];
pii f[1 << maxm], g[1 << maxm];
int cnt[maxm];
int S;

bool cmp(int x, int y) {
	return p[b[x] & S] > p[b[y] & S];
}

pii merge(pii x, pii y) {
	vector<int> u;
	if (x.fs) u.push_back(x.fs); if (x.sc) u.push_back(x.sc);
	if (y.fs) u.push_back(y.fs); if (y.sc) u.push_back(y.sc);
	sort(u.begin(), u.end(), cmp); u.resize(unique(u.begin(), u.end()) - u.begin());
	pii ans;
	if (u.size() >= 2) ans = {u[0], u[1]};
	else ans = {(u.size() ? u[0] : 0), 0};
	return ans;
}

void fake_main() {
	pii x;
	n = read(), m = read(), msk = (1 << m) - 1;
	rep(i, 1, n) rep(j, 0, m - 1) a[i] |= ((tmp = read()) << j), cnt[j] += tmp;
	rep(i, 1, n) b[i] = msk ^ a[i], g[b[i]] = merge(g[b[i]], {i, 0});
	rep(i, 0, m) rep(s, 0, msk) if (!(s >> i & 1)) g[s] = merge(g[s], g[s ^ (1 << i)]);
	rep(s, 0, msk) f[s] = g[s], p[s] = __builtin_popcount(s);
	rep(i, 0, m) rep(s, 0, msk) if (s >> i & 1) S = s, f[s] = merge(f[s], f[s ^ (1 << i)]);
	rep(i, 1, n) {
		int s = 0, ans = 0, qwq = 0;
		rep(j, 0, m - 1) {
			tmp = cnt[j] - (a[i] >> j & 1);
			if (tmp == n / 2) s |= (1 << j);
			else if (tmp > n / 2) ++ans;
		}
		if (f[s].fs == i) write(ans + p[b[f[s].sc] & s]), pc('\n');
		else write(ans + p[b[f[s].fs] & s]), pc('\n');
	}
}

signed main() {
	int T = multidata ? read() : 1;
	while (T--) fake_main();
	return 0;
}
原文