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 = 3e5;
const int M = 20;
pair<int,int> f[1<<M][2];
int f2[M];
int v[N];
void add(int pos,int x,int id){
for(int i = 0;i < 2;i++){
if(f[pos][i].second == id){
f[pos][i].first = max(f[pos][i].first, x);
if(f[pos][0].first < f[pos][1].first)swap(f[pos][0],f[pos][1]);
return;
}
}
for(int i = 0;i < 2;i++){
if(pair<int,int>{x,id} > f[pos][i]){
pair<int,int> tmp = f[pos][i];
f[pos][i] = {x,id};
//cout<<x<<' '<<id<<' '<<pos<<' '<<i<<"ll\n";
x = tmp.first;
id = tmp.second;
}
}
}
int main(){
int n,m;
cin>>n>>m;
for(int i = 0;i < n;i++){
int nr = 0;
int cnt = 0;
for(int j = 0;j < m;j++){
char ch;
cin>>ch;
nr = nr*2 + ch - '0';
f2[j]+=ch - '0';
}
v[i] = nr;
add((nr^((1<<m) - 1)),__builtin_popcount((nr^((1<<m) - 1))),i + 1);
}
for(int i = (1<<m) - 1;i >= 0;i--){
for(int j = 0;j < m;j++){
if(i>>j&1){
add(i^(1<<j),f[i][0].first - 1,f[i][0].second);
add(i^(1<<j),f[i][1].first - 1,f[i][1].second);
//f[i^(1<<j)] = f[i] - 1;
}
}
}
for(int i = 0;i < (1<<m);i++){
for(int j = 0;j < m;j++){
if(!(i>>j&1)){
add(i^(1<<j),f[i][0].first,f[i][0].second);
add(i^(1<<j),f[i][1].first,f[i][1].second);
}
}
//cout<<f[i][0].first<<' '<<f[i][1].first<<' '<<f[i][0].second<<' '<<f[i][1].second<<'\n';
}
for(int i = 0;i < n;i++){
int ans = 0;
int nr2 = 0;
for(int j = 0;j < m;j++){
bool ok = ((v[i]>>(m - j - 1))&1);
int nr = f2[j] - ok - 1;
//cout<<nr<<' '<<n/2<<'\n';
if(nr + 1 == n/2){
nr2+=(1<<(m - j - 1));
}else{
ans+=bool(nr + 1 > n/2);
}
}
//cout<<nr2<<' '<<f[nr2][0].first<<' '<<f[nr2][0].second<<' '<<f[nr2][1].first<<' '<<f[nr2][1].second<<'\n';
if(f[nr2][0].second != i + 1){
ans+=f[nr2][0].first;
}else{
ans+=f[nr2][1].first;
}
cout<<ans<<'\n';
}
return 0;
}
Compilation message (stderr)
council.cpp: In function 'int main()':
council.cpp:31:13: warning: unused variable 'cnt' [-Wunused-variable]
31 | int cnt = 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... |