#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define ff first
#define ss second
#define ln "\n"
#define mp make_pair
#define pb push_back
#define INF (ll)2e18
#define MOD (ll)(1e9+7)
void solve(){
ll n, m; cin >> n >> m;
vector<vector<pair<ll, ll>>> gr(1<<m);
for (ll i=0; i<(1<<m); i++){
for (ll j=0; j<m; j++){
if (i&(1<<j)){
gr[i].push_back({i^(1<<j), 1});
}else{
gr[i].push_back({i^(1<<j), 0});
}
}
}
vector<ll> vals(n);
vector<vector<pair<ll, ll>>> cost(1<<m, vector<pair<ll, ll>>(2, {INF, -1}));
priority_queue<pair<pair<ll, ll>, pair<ll, ll>>, vector<pair<pair<ll, ll>, pair<ll, ll>>>, greater<pair<pair<ll, ll>, pair<ll, ll>>>> que;
for (ll i=0; i<n; i++){
ll res=0; for (ll j=0; j<m; j++){
ll x; cin >> x;
if (x) res+=(1<<j);
}
vals[i]=res;
que.push({{0, res}, {0, i}});
cost[res][0] = {0, i};
}
while (!que.empty()){
ll u, w; tie(w, u) = que.top().ff;
ll o, cf; tie(o, cf) = que.top().ss;
que.pop(); if (cost[u][o].ff<=w) continue;
for (auto [v, c]:gr[u]){
if (cost[v][0].ff>c+w) {
que.push({{c+w, v}, {0, cf}});
cost[v][0]={c+w, cf};
}else if (cost[v][1].ff>c+w and cf!=cost[v][0].ss){
que.push({{c+w, v}, {1, cf}});
cost[v][1]={c+w, cf};
}
}
}
vector<ll> cnt(m);
for (ll i=0; i<n; i++) for (ll j=0; j<m; j++) if (vals[i]&(1<<j)) cnt[j]++;
for (ll i=0; i<n; i++){
for (ll j=0; j<m; j++){
if (vals[i]&(1<<j)) cnt[j]--;
}
ll res=0, find=0;
for (ll j=0; j<m; j++){
if (cnt[j]>n/2){
res++; find+=(1<<j);
}else if (cnt[j]<n/2){
find+=(1<<j);
}else if (cnt[j]==n/2){
res++;
}
}
// cout << find << ": ";
cout << res-(cost[find][0].ss==i?cost[find][1].ff:cost[find][0].ff) << ln;
for (ll j=0; j<m; j++){
if (vals[i]&(1<<j)) cnt[j]++;
}
}
}
/*
9-2
101110010001
*/
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#ifdef LOCAL
auto start = chrono::high_resolution_clock::now();
#endif
ll t=1;
// cin >> t;
for (ll c=1; c<=t; c++) solve();
#ifdef LOCAL
auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
cout << setprecision(0) << fixed << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
#endif
}
# | 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... |