This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 3e5+10, MOD = 1e9+7;
const int M = 21;
int n, m, a[N], cnt[M];
// minimal number of things to turn off so that it becomes a subset of ~bad
ar<ar<int, 2>, 2> best[1 << M], sub[1 << M];
ar<ar<int, 2>, 2> add(ar<ar<int, 2>, 2> base, ar<int, 2> x) {
for (ar<int, 2> y : base) if (y[1] == x[1]) return base;
if (x < base[0]) {
swap(base[0], base[1]);
base[0] = x;
} else if (x < base[1]) {
base[1] = x;
}
return base;
}
ar<ar<int, 2>, 2> add(ar<ar<int, 2>, 2> base, ar<ar<int, 2>, 2> x) {
return add(add(base, x[0]), x[1]);
}
void solve() {
cin >> n >> m;
ar<ar<int, 2>, 2> INF; INF.fill(ar<int, 2>{MOD, MOD});
fill(best, best + (1 << m), INF);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int x; cin >> x;
a[i] |= x << j;
cnt[j] += x;
}
best[a[i]] = add(best[a[i]], ar<int, 2>{__builtin_popcount(a[i]), i});
}
for (int j = 0; j < m; j++) {
for (int i = (1 << m) - 1; i >= 0; i--) if ((i >> j) & 1 ^ 1) {
best[i] = add(best[i], best[i ^ (1 << j)]);
}
}
fill(sub, sub + (1 << m), INF);
for (int i = 0; i < (1 << m); i++) {
for (auto [x, c] : best[i]) if (c < MOD) {
int cost = x - __builtin_popcount(i);
sub[i] = add(sub[i], ar<int, 2>{cost, c});
}
}
for (int j = 0; j < m; j++) {
for (int i = 0; i < (1 << m); i++) if ((i >> j) & 1) {
sub[i] = add(sub[i], sub[i ^ (1 << j)]);
}
}
for (int i = 0; i < n; i++) {
int base = 0;
int bad = 0;
for (int j = 0; j < m; j++) {
int need = n / 2;
int has = cnt[j] - ((a[i] >> j) & 1);
if (has != need) {
base += has > need;
continue;
}
bad |= 1 << j;
base++;
}
int ans = m;
int can_sub = (1 << m) - 1 - bad;
for (auto [x, c] : sub[can_sub]) if (c != i) {
ans = min(ans, x);
}
// for (int j = 0; j < n; j++) if (j != i) ans = min(ans, __builtin_popcount(a[j] & bad));
cout << base - ans << '\n';
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (T--) solve();
}
Compilation message (stderr)
council.cpp: In function 'void solve()':
council.cpp:48:62: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
48 | for (int i = (1 << m) - 1; i >= 0; i--) if ((i >> j) & 1 ^ 1) {
| ~~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |