#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!=res.ind1) {
res.ans2=res.ans1;
res.ind2=res.ind1;
}
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].first=s.first;
if (s.second!=s.first) idx[mask][m].second=s.second;
} else if (idx[mask][m].second==0) {
if (s.first!=idx[mask][m].second) idx[mask][m].second=s.first;
else if (s.second!=idx[mask][m].second) idx[mask][m].second=s.second;
}
}
//cout << mask << ' ' << m << ' ' << idx[mask][m].first << ' ' << idx[mask][m].second << endl;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
365 ms |
10488 KB |
Output is correct |
3 |
Correct |
349 ms |
10324 KB |
Output is correct |
4 |
Correct |
353 ms |
9904 KB |
Output is correct |
5 |
Correct |
355 ms |
10524 KB |
Output is correct |
6 |
Incorrect |
345 ms |
10064 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
365 ms |
10488 KB |
Output is correct |
3 |
Correct |
349 ms |
10324 KB |
Output is correct |
4 |
Correct |
353 ms |
9904 KB |
Output is correct |
5 |
Correct |
355 ms |
10524 KB |
Output is correct |
6 |
Incorrect |
345 ms |
10064 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
365 ms |
10488 KB |
Output is correct |
3 |
Correct |
349 ms |
10324 KB |
Output is correct |
4 |
Correct |
353 ms |
9904 KB |
Output is correct |
5 |
Correct |
355 ms |
10524 KB |
Output is correct |
6 |
Incorrect |
345 ms |
10064 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
365 ms |
10488 KB |
Output is correct |
3 |
Correct |
349 ms |
10324 KB |
Output is correct |
4 |
Correct |
353 ms |
9904 KB |
Output is correct |
5 |
Correct |
355 ms |
10524 KB |
Output is correct |
6 |
Incorrect |
345 ms |
10064 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |