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;
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;
for (int i = 1;i <= n;i++) {
for (int j = 0;j < m;j++) {
int x;
cin >> x;
if (x > 0) {
a[i] += (1 << j);
cnt[j]++;
}
}
}
int k = (m + 1) / 2;
// for (int i = 0;i < (1 << k);i++) {
// for (int j = 0;j < (1 << k);j++) {
// kurac[i][j] = __builtin_popcount(i & j);
// bst[i][j] = {{(1 << m) - 1, m + 1}, {(1 << m) - 1, m + 1}};
// }
// }
for (int j = 1;j <= n;j++) {
pr = (a[j] & ((1 << (m - k)) - 1));
gas = (a[j] >> (m - k));
for (int i = 0;i < (1 << k);i++) {
bt = kurac[gas][i];
if (bt < bst[pr][i].f.s) {
bst[pr][i].s = bst[pr][i].f;
bst[pr][i].f = {a[j], bt};
}
else if (bt < bst[pr][i].s.s)
bst[pr][i].s = {a[j], 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 / 2) {
b += (1 << j);
c++;
}
else if (gas > n / 2)
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 + kurac[pr][j]);
else
s = min(s, bst[j][suf].s.s + kurac[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... |