제출 #1095473

#제출 시각아이디문제언어결과실행 시간메모리
1095473_8_8_Council (JOI23_council)C++17
100 / 100
511 ms55376 KiB
#include <bits/stdc++.h>
    
using namespace std;
    
typedef long long ll;
const int  N = 3e5 + 12, MOD = (int)1e9 + 7, M = (1 << 20) + 12;

int n, m, c[N], a[N][21], s[N], f[M][2], who[M][2];

void u(int mask, int val = -1, int from = -1) {
    if(val == -1) {
        val = __builtin_popcount(mask);
    }
    if(from == who[mask][0]) {
        f[mask][0] = max(f[mask][0], val);
        return;
    }
    if(f[mask][0] < val) {
        f[mask][1] = f[mask][0];
        who[mask][1] = who[mask][0];
        f[mask][0] = val;
        who[mask][0] = from;
    } else {
        if(val > f[mask][1]) {
            f[mask][1] = val;
            who[mask][1] = from;
        }
    }
} 
void test() {
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < m; j++) {
            cin >> a[i][j];
            if(a[i][j]) {
                c[j]++;
            } else {
                s[i] += (1 << j);
            }
        }
        u(s[i], -1, i);
    }

    for(int i = 0; i < m; i++) {
        for(int j = 0; j < (1 << m); j++) {
            if(!((j >> i) & 1)) {
                int k = (j ^ (1 << i));
                if(f[k][0]) {
                    u(j, -1, who[k][0]);
                }
                if(f[k][1]) {
                    u(j, -1, who[k][1]);
                }
            }
        }
    }
    for(int i = 0; i < m; i++) {
        for(int j = 0; j < (1 << m); j++) {
            if(((j >> i) & 1)) {
                int k = (j ^ (1 << i));
                u(j, f[k][0], who[k][0]);
                u(j, f[k][1], who[k][1]);
            }       
        }
    }
    int val = n / 2;
    for(int i = 1; i <= n; i++) {
        int t = 0, cnt = 0;
        for(int j = 0; j < m; j++) {
            int k  = c[j] - a[i][j];
            if(k == val) {
                t += (1 << j);
            } else if(k > val) {
                cnt++;
            }
        }
        // int mx = 0;
        // for(int j = 1; j <= n; j++) {
        //     if(j == i) continue;
        //     mx = max(mx, __builtin_popcount(s[j] & t));
        // }
        int ff = f[t][0];
        if(who[t][0] == i) {
            ff = f[t][1];
        }
        cout << ff + cnt << '\n';
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); 

    int t = 1; 
    // cin >> t;
    
    while(t--) 
        test();
    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...