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;
typedef pair<int, int> pi;
int cost(int x, int y){
return __builtin_popcount(x & y);
}
int n, m;
pi combine(pi a, pi b){
if(a.first == -1) return b;
if(a.second == -1) return {a.first, b.first};
return a;
}
vector<pi> best;
void rec(int msk, int i, const vector<vector<pi>>& v){
if(i == m){
for(pi p : v[0]){
best[msk] = combine(best[msk], p);
}
return;
}
vector<vector<pi>> a(1<<(m-i-1), vector<pi>(i+2, {-1, -1}));
vector<vector<pi>> b = a;
for(int j = 0; j < (1<<(m-i)); j++){
for(int k = 0; k <= i; k++){
a[j>>1][k] = combine(a[j>>1][k], v[j][k]);
int add = j&1;
b[j>>1][k+add] = combine(b[j>>1][k+add], v[j][k]);
}
}
rec(msk, i+1, a);
rec(msk|(1<<i), i+1, b);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
vector<int> a(n);
vector<int> total(m);
vector<vector<pi>> v(1<<m, vector<pi>(1, {-1, -1}));
best.assign(1<<m, {-1, -1});
for(int i = 0; i < n; i++){
int x = 0, y;
for(int j = 0; j < m; j++){
cin >> y;
x += y*(1<<j);
total[j] += y;
}
a[i] = x;
v[x][0] = combine(v[x][0], {x, -1});
}
rec(0, 0, v);
for(int i : a){
int ans = 0;
int crit = 0;
for(int j = 0; j < m; j++){
int votes = total[j];
if(i & (1<<j)) votes--;
if(votes >= n/2){
ans++;
}
if(votes == n/2){
crit |= (1<<j);
}
}
int j = best[crit].first;
if(i == j) j = best[crit].second;
ans -= cost(crit, j);
cout << ans << "\n";
}
}
# | 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... |