#include <bits/stdc++.h>
using namespace std;
const int nx=3e5+5, mx=20;
int n, m, cnt[mx], x, vl[nx], rvl[nx];
pair<pair<int, int>, pair<int, int>> dp[(1<<mx)];
void add(int msk, pair<int, int> x)
{
//if (msk==3) cout<<"add "<<x.first<<' '<<x.second<<'\n';
if (x.first>=dp[msk].first.first)
{
if (x.second!=dp[msk].first.second) swap(dp[msk].first, dp[msk].second), dp[msk].first=x;
else dp[msk].first=x;
}
else if (x.first>=dp[msk].second.first)
{
if (x.second==dp[msk].first.second) return;
else dp[msk].second=x;
}
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=n; i++) for (int j=0; j<m; j++) cin>>x, vl[i]+=x*(1<<j), cnt[j]+=x, rvl[i]+=(!x)*(1<<j);
//for (int j=0; j<m; j++) cout<<"cnt "<<j<<' '<<cnt[j]<<'\n';
for (int i=1; i<=n; i++) add(rvl[i], {__builtin_popcount(rvl[i]), i}); //cout<<"rvl "<<i<<' '<<rvl[i]<<'\n';
for (int i=(1<<m)-1; i>=0; i--)
{
for (int j=0; j<m; j++)
{
if (!(i&(1<<j)))
{
int msk=(i+(1<<j));
if (dp[msk].first.second) add(i, {dp[msk].first.first-1, dp[msk].first.second});
if (dp[msk].second.second) add(i, {dp[msk].second.first-1, dp[msk].second.second});
}
}
}
for (int i=0; i<(1<<m); i++)
{
for (int j=0; j<m; j++)
{
if ((i&(1<<j)))
{
int msk=(i-(1<<j));
add(i, dp[msk].first);
add(i, dp[msk].second);
}
}
//cout<<"dp "<<i<<' '<<dp[i].first.first<<' '<<dp[i].first.second<<' '<<dp[i].second.first<<' '<<dp[i].second.second<<'\n';
}
for (int i=1; i<=n; i++)
{
int msk=0, ans=0;
for (int j=0; j<m; j++)
{
int cur=cnt[j]-((vl[i]&(1<<j))>0);
//cout<<i<<' '<<j<<' '<<cur<<'\n';
if (cur>n/2) ans++;
else if (cur==n/2) msk|=(1<<j);
}
bitset<12> b(msk);
//cout<<"debug "<<i<<' '<<b<<'\n';
if (dp[msk].first.second!=i) cout<<ans+dp[msk].first.first<<'\n';
else cout<<ans+dp[msk].second.first<<'\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... |