제출 #1335659

#제출 시각아이디문제언어결과실행 시간메모리
1335659yhkhooCouncil (JOI23_council)C++17
0 / 100
485 ms182788 KiB
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
#define fi first
#define se second
#define pop __builtin_popcount

const int MAXN = 300000, MAXM = 21, MAXM2 = (1<<20), INF = MAXN*67;
int n, m;
int a[MAXN], s[MAXM];
pii dp1[MAXM2], dp2[MAXM2][MAXM];

void merge(pii& a, pii& b){
    if(a.fi == -1){
        a = b;
    }
    else{
        if(b.fi != -1 && a.fi != b.fi){
            a.se = b.fi;
        }
        if(b.se != -1 && a.fi != b.se){
            a.se = b.se;
        }
    }
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    memset(dp1, -1, sizeof(dp1));
    memset(dp2, -1, sizeof(dp2));
    int x = n/2;
    for(int i=0; i<n; i++){
        for(int j=0, aij; j<m; j++){
            cin >> aij;
            if(aij){
                s[j]++;
            }
            else{
                a[i] ^= (1<<j);
            }
        }
    }
    for(int i=0; i<n; i++){
        if(dp1[a[i]].fi == -1){
            dp1[a[i]].fi = i;
        }
        else{
            dp1[a[i]].se = i;
        }
    }
    for(int k=0; k<m; k++){
        for(int i=0; i<(1<<m); i++){
            if(!(i & (1<<k))){
                merge(dp1[i], dp1[i^(1<<k)]);
            }
        }
    }
    for(int i=0; i<(1<<m); i++){
        dp2[i][__builtin_popcount(i)] = dp1[i];
    }
    for(int k=0; k<m; k++){
        for(int i=0; i<(1<<m); i++){
            if((i & (1<<k))){
                for(int j=0; j<=m; j++){
                    merge(dp2[i][j], dp2[i^(1<<k)][j]);
                }
            }
        }
    }
    int tot = 0, r = 0;
    for(int i=0; i<m; i++){
        if(s[i] >= x){
            tot++;
        }
        if(s[i] == x){
            r ^= (1<<i);
        }
    }
    for(int i=0; i<n; i++){
        int cr = r, d = 0;
        for(int j=0; j<m; j++){
            if((a[i] & (1<<j))) continue;
            if(s[j] == x){
                cr ^= (1<<j);
                d++;
            }
            else if(s[j] == x+1){
                cr ^= (1<<j);
            }
        }
        int f = cr & a[i];
        int fc = pop(f);
        int nc = 0;
        for(int k=m-1; k>=0; k--){
            if(dp2[cr][k].fi != -1){
                if(dp2[cr][k].fi == i && dp2[cr][k].se == -1){
                    continue;
                }
                nc = k;
                break;
            }
        }
        cout << tot - d - pop(cr) + nc << '\n';
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...