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;
const int NM = 3e5, MM = 20;
int N, M, msk[NM+5], cnt[(1<<MM)+5], f[(1<<MM)+5], g[(1<<MM)+5];
int occ[MM+5];
void optimize(int i, int x){
if (x == f[i] || x == g[i]) return;
if (f[i] == -1 || __builtin_popcount(x&i) > __builtin_popcount(f[i]&i)){
f[i] = x;
}
else if (g[i] == -1 || __builtin_popcount(x&i) > __builtin_popcount(g[i]&i)){
g[i] = x;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
for (int i = 1; i <= N; i++){
for (int j = 0; j < M; j++){
char ch; cin >> ch;
if (ch == '1'){
msk[i] += (1<<j);
occ[j]++;
}
}
cnt[msk[i]]++;
}
for (int i = 0; i < (1<<M); i++){
if (cnt[i] > 0) f[i] = i; else f[i] = -1;
g[i] = -1;
}
for (int j = 0; j < M; j++)
for (int i = (1<<M)-1; i >= 0; i--)
if ((i>>j)&1){
if (f[i-(1<<j)] > f[i]){
g[i] = f[i];
f[i] = f[i-(1<<j)];
}
else if (f[i-(1<<j)] < f[i] && f[i-(1<<j)] > g[i]){
g[i] = f[i-(1<<j)];
}
if (g[i-(1<<j)] < f[i] && g[i-(1<<j)] > g[i]){
g[i] = g[i-(1<<j)];
}
}
for (int i = 0; i < (1<<M); i++){
if (f[i] != -1) f[i] = (1<<M)-1-f[i];
if (g[i] != -1) g[i] = (1<<M)-1-g[i];
}
for (int i = 0; i < (1<<(M-1)); i++){
swap(f[i], f[(1<<M)-1-i]);
swap(g[i], g[(1<<M)-1-i]);
}
for (int j = 0; j < M; j++)
for (int i = (1<<M)-1; i >= 0; i--)
if ((i>>j)&1){
optimize(i, f[i-(1<<j)]);
optimize(i, g[i-(1<<j)]);
}
for (int i = 0; i < (1<<M); i++){
if (f[i] != -1) f[i] = (1<<M)-1-f[i];
if (g[i] != -1) g[i] = (1<<M)-1-g[i];
}
for (int i = 1; i <= N; i++){
int tmp = 0, res = 0;
for (int j = 0; j < M; j++)
if (occ[j]-((msk[i]>>j)&1) >= N/2){
res++;
if (occ[j]-((msk[i]>>j)&1) == N/2) tmp += (1<<j);
}
if (f[tmp] == msk[i] && cnt[msk[i]] == 1)
cout << res-__builtin_popcount(g[tmp]&tmp) << '\n';
else cout << res-__builtin_popcount(f[tmp]&tmp) << '\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... |