이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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();
}
컴파일 시 표준 에러 (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... |