제출 #1162941

#제출 시각아이디문제언어결과실행 시간메모리
1162941pemguimnCouncil (JOI23_council)C++20
100 / 100
415 ms18804 KiB
#include <bits/stdc++.h>

#define pii pair<int, int>

using namespace std;

const int N = 3e5 + 5, M = 20, INF = 1e9 + 7;
int n, m, a[N], voted[M];

pii dp[2][1 << M];

inline void add(int x, pii y){
    if(y.first == INF || y.second == dp[0][x].second || y.second == dp[1][x].second) return;
    if(y < dp[0][x]) dp[1][x] = dp[0][x], dp[0][x] = y;
    else dp[1][x] = min(dp[1][x], y);
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    cin >> n >> m;
    int mask = 1 << m, cutoff = n / 2; 

    int z = 0, o = 0, t = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            int x; cin >> x;
            a[i] |= (x << j);
            voted[j] += x;
        }
    }   

    int cnt = 0;
    for(int j = 0; j < m; j++){
        if(voted[j] > cutoff + 1) t |= (1 << j);
        else if(voted[j] == cutoff + 1) o |= (1 << j);
        else if(voted[j] == cutoff) z |= (1 << j);

        if(voted[j] >= cutoff) cnt++;
    }

    for(int i = 0; i < (1 << m); i++){
        dp[0][i] = dp[1][i] = {INF, INF};
    }

    for(int i = 0; i < n; i++){
        a[i] = (a[i] & ((mask - 1) ^ t));
        pii cur = {0, i};
        add(a[i], cur);
    }

    for(int i = 0; i < m; i++){
        for(int j = 0; j < mask; j++){
            if(j & (1 << i)){
                add(j, dp[0][j ^ (1 << i)]);
                add(j, dp[1][j ^ (1 << i)]);
            }
        }
    }

    for(int i = 0; i < m; i++){
        for(int j = 0; j < mask; j++){
            if(j & (1 << i)){
                add(j ^ (1 << i), {dp[0][j].first + 1, dp[0][j].second});
                add(j ^ (1 << i), {dp[1][j].first + 1, dp[1][j].second});
            }
        }
    }

    for(int i = 0; i < n; i++){
        int qq = (mask - 1) ^ ((z ^ (a[i] & z)) | (o & a[i]));
        int v = (dp[0][qq].second == i ? dp[1][qq].first : dp[0][qq].first);
        cout << cnt - v - __builtin_popcount(z & a[i]) << '\n';
    }
}
#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...