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;
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int N = 3e5;
int ans[N+10];
pair<int, int> dp[2][1025][1025];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m; cin >> n >> m;
vector< vector<int> > a(n, vector<int>(m));
for(int i = 0;i < n; i++){
for(int j = 0;j < m; j++){
cin >> a[i][j];
}
}
vector<int> cnt(m,0);
for(int i = 0;i < n; i++){
for(int j = 0;j < m; j++){
cnt[j]+= a[i][j];
}
}
vector<int> bits(n);
for(int i = 0;i < n; i++){
int mask = 0;
for(int j = 0;j < m; j++){
if(a[i][j]){
mask+= (1<<j);
}
}
bits[i] = mask;
}
for(int i = 0;i < 2; i++){
for(int j = 0;j < 1024; j++){
for(int k = 0;k < 1024; k++){
dp[i][j][k] = {-1, -1};
}
}
}
for(int i = 0;i < n; i++){
int m0 = bits[i] & 1023;
int m1 = (bits[i]>>10);
for(int j = 0;j < 1024; j++){
int c = __builtin_popcount(m1 & j);
//cout << c << '\n';
if(dp[0][m0][j].ff == -1){
dp[0][m0][j] = {c, i};
}else{
if(dp[0][m0][j].ff >= c){
dp[1][m0][j] = dp[0][m0][j];
dp[0][m0][j] = {c, i};
}else if(dp[1][m0][j].ff == -1 || dp[1][m0][j].ff >= c){
dp[1][m0][j] = {c, i};
}
}
}
}
for(int i = 0;i < n; i++){
int mask = 0, bc = 0;
for(int j = 0;j < m; j++){
if(bits[i] & (1<<j)){
if(cnt[j] == n/2 + 1){
bc++;
mask+= (1<<j);
}else if(cnt[j] > n/2+1){
bc++;
}
}else{
if(cnt[j] > n / 2) bc++;
else if(cnt[j] == n / 2){
mask+= (1<<j);
bc++;
}
}
}
//cout << i+1 << " = " << bc << '\n';
//cout << i+1 << " = " << mask << '\n';
int mn = 20;
int m0 = mask & 1023;
int m1 = mask >> 10;
for(int j = 0;j < 1024; j++){
int c = __builtin_popcount(m0 & j);
if(dp[0][j][m1].ff == -1) continue;
int kek = 20;
if(dp[0][j][m1].ss != i && dp[0][j][m1].ss != -1){
kek = min(kek, dp[0][j][m1].ff);
}
if(dp[1][j][m1].ss != i && dp[1][j][m1].ss != -1){
kek = min(kek, dp[1][j][m1].ff);
}
//cout << kek << "\n";
mn = min(mn, kek + c);
}
//cout << i+1 << " = " << mn << '\n';
ans[i] = ans[i] + bc - mn;
}
for(int i = 0;i < n; i++) cout << ans[i] << '\n';
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... |