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 int long long
#define f first
#define s second
const int N = 3e5 + 69;
const int M = 21;
int a[N], cnt[M], n, m;
int t[M], k, gas, pr, suf, bt, b, ans, c, s, x;
vector<pair<int, int>> g[1030];
pair<pair<int, int>, pair<int, int>> bst[1030][1030];
int kurac[1030][1030];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> m;
k = (m + 1) / 2;
for (int i = 1;i <= n;i++) {
for (int j = 0;j < m;j++) {
cin >> x;
if (x > 0) {
a[i] |= (1 << j);
cnt[j]++;
}
}
pr = (a[i] & ((1 << (m - k)) - 1));
suf = (a[i] >> (m - k));
g[pr].push_back({suf, a[i]});
}
for (int i = 0;i < (1 << k);i++) {
for (int j = 0;j < (1 << k);j++) {
bst[i][j] = {{(1 << m) - 1, m + 1}, {(1 << m) - 1, m + 1}};
for (auto x : g[i]) {
int bt = __builtin_popcount(x.f & j);
if (bt < bst[i][j].f.s) {
bst[i][j].s = bst[i][j].f;
bst[i][j].f = {x.s, bt};
}
else if (bt < bst[i][j].s.s)
bst[i][j].s = {x.s, bt};
}
}
}
//sufix duzine k, prefix duzine m - k
for (int i = 1;i <= n;i++) {
b = ans = c = 0;
for (int j = 0;j < m;j++) {
gas = cnt[j] - ((a[i] & (1 << j)) > 0);
if (gas == (n >> 1)) {
b |= (1 << j);
c++;
}
else if (gas > (n >> 1))
ans++;
}
suf = (b >> (m - k)), s = c;
pr = (b & ((1 << (m - k)) - 1));
for (int j = 0;j < (1 << (m - k));j++) {
if (a[i] != bst[j][suf].f.f)
s = min(s, bst[j][suf].f.s + __builtin_popcount(pr & j));
else
s = min(s, bst[j][suf].s.s + __builtin_popcount(pr & j));
}
ans += c - s;
cout << ans << '\n';
}
}
return 0;
}
# | 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... |