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;
const int N = 300001;
const int M = 20;
const int to = 1024;
int a[N], sum[M], masks[N];
unsigned char dp[to][to], ppcnt[to][to], mn[N], ans[N];
short l[1<<M], r[1<<M];
void smin(unsigned char & x, unsigned char y){
if(x>y)x=y;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for(int i=0; i< n; i++){
for(int j=0; j< m; j++){
int x;
cin >> x;
a[i]+=x<<j;
sum[j]+=x;
}
}
for(int i=0; i< n; i++)mn[i]=M;
for(int i=0; i< n; i++){
for(int j=0; j< m; j++)sum[j]-=(a[i] & (1<<j))>>j;
for(int j=0; j< m; j++){
if(sum[j]>=n/2)ans[i]++;
if(sum[j]==n/2)masks[i]+=1<<j;
}
for(int j=0; j< m; j++)sum[j]+=(a[i] & (1<<j))>>j;
}
for(int i=0; i< (1<<M); ++i){
l[i]=i>>10;
r[i]=i & ((1<<10)-1);
short f = l[i], s = r[i];
dp[f][s]=M;
ppcnt[f][s]=__builtin_popcount(f & s);
}
for(int i=0; i< n-1; ++i){
short f = l[a[i]];
short s = r[a[i]];
for(short j = 0; j<(1<<(max(0, m-10))); ++j){
smin(dp[j][s], ppcnt[j][f]);
}
f = l[masks[i+1]];
s = r[masks[i+1]];
for(short j=0; j<to; ++j){
smin(mn[i+1], dp[f][j]+ppcnt[s][j]);
}
}
for(int i=0; i< (1<<M); ++i)dp[l[i]][r[i]]=M;
for(int i=n-1; i>0; i--){
short f = l[a[i]];
short s = r[a[i]];
for(short j = 0; j<(1<<(max(0, m-10))); ++j){
smin(dp[j][s], ppcnt[j][f]);
}
f = l[masks[i-1]];
s = r[masks[i-1]];
for(short j=0; j<to; ++j){
smin(mn[i-1], dp[f][j]+ppcnt[s][j]);
}
}
for(int i=0; i< n; i++){
cout << ans[i]-mn[i] << '\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... |