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 <iostream>
using namespace std;
const int MAXN=3e5+10;
const int MAXM=(1<<20);
//#define endl '\n'
struct Answer {
int ans1, ind1;
int ans2, ind2;
};
int n, M;
int a[MAXN], op[MAXN], cnt[32];
pair <int, int> idx[MAXM][25], base[MAXM];
Answer dp[MAXM][25];
bool mem1[MAXM][25], mem2[MAXM][25];
bool fbit(int x, int bit) {
return (x & (1<<bit));
}
Answer find_best (Answer res, int s, int i) {
if (s>=res.ans1) {
if (res.ind2!=i) {
res.ans2=res.ans1;
res.ind2=res.ind1;
} else res.ans2=res.ind2=0;
res.ans1=s;
res.ind1=i;
} else if (s>=res.ans2 && i!=res.ind1) {
res.ans2=s;
res.ind2=i;
}
return res;
}
pair <int,int> find_idx(int mask, int m) {
if (m==-1) return base[mask];
if (mem1[mask][m]) return idx[mask][m];
mem1[mask][m]=true;
idx[mask][m]=find_idx(mask,m-1);
if (!(mask & (1 << m))) {
pair <int, int> s=find_idx((mask ^ (1 << m)),m-1);
if (idx[mask][m].first==0) idx[mask][m]=s;
else if (idx[mask][m].second==0) {
if (s.first && s.first!=idx[mask][m].first) idx[mask][m].second=s.first;
else if (s.second && s.second!=idx[mask][m].first) idx[mask][m].second=s.second;
}
}
return idx[mask][m];
}
Answer find_dp(int mask, int m) {
if (m==-1) {
Answer res={0,0,0,0};
pair <int,int> cur=find_idx(mask,M-1);
int s=__builtin_popcount(mask);
if (cur.first!=0) {
res.ans1=s;
res.ind1=cur.first;
}
if (cur.second!=0 && cur.second!=res.ind1) {
res.ans2=s;
res.ind2=cur.second;
}
return res;
}
if (mem2[mask][m]) return dp[mask][m];
mem2[mask][m]=true;
dp[mask][m]=find_dp(mask,m-1);
if ((mask & (1 << m))) {
Answer res=find_dp((mask ^ (1 << m)),m-1);
dp[mask][m]=find_best(dp[mask][m],res.ans1,res.ind1);
dp[mask][m]=find_best(dp[mask][m],res.ans2,res.ind2);
}
//cout << "dp " << mask << ' ' << m << ' ' << dp[mask][m].ans1 << ' ' << dp[mask][m].ans2 << ' ' << dp[mask][m].ind1 << ' ' << dp[mask][m].ind2 << endl;
return dp[mask][m];
}
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> M;
int bit;
for (int i=1;i<=n;i++) {
for (int j=0;j<M;j++) {
cin >> bit;
a[i]+=(bit<<j);
cnt[j]+=bit;
}
op[i]=(((1 << M) - 1) & (~a[i]));
if (base[op[i]].first==0) base[op[i]].first=i;
else base[op[i]].second=i;
}
for (int i=1;i<=n;i++) {
int ans=0, mask=0;
for (int j=0;j<M;j++) {
if (cnt[j]>=n/2+2) ans++;
else if (cnt[j]==n/2+1) {
if (!fbit(a[i],j)) ans++;
else mask+=(1<<j);
} else if (cnt[j]==n/2 && !fbit(a[i],j)) mask+=(1<<j);
}
Answer res=find_dp(mask,M-1);
if (res.ind1!=i) ans+=res.ans1;
else ans+=res.ans2;
cout << ans << endl;
}
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... |