Submission #1335793

#TimeUsernameProblemLanguageResultExecution timeMemory
1335793WongYiKaiCouncil (JOI23_council)C++20
100 / 100
1874 ms300440 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
    ll n,m;
    cin >> n >> m;
    ll a[n][m];
    vector<pair<ll,ll>> dp[1<<m][2];
    vector<ll> guy[1<<m];
    // is it possible to make 01110101 where 1s can be 0s
    ll sum[m+1];
    memset(sum,0,sizeof(sum));
    for (int i=0;i<n;i++){
        ll num = 0;
        ll cnt = 0;
        for (int j=0;j<m;j++){
            cin >> a[i][j];
            num += (a[i][j]<<j);
            if (a[i][j]==1) cnt++;
            sum[j] += a[i][j];
        }
        if (guy[num].size() < 2) guy[num].push_back(i);
    }
    for (int i=0;i<(1<<m);i++){
        ll zero = m-__builtin_popcount(i);
        for (ll guyy:guy[i]){
            if (dp[i][0].size() < 2) dp[i][0].push_back({zero,guyy});
        }
    }
    for (int k=1;k<=m;k++){
        for (int i=0;i<(1<<m);i++){
            ll zero = m-__builtin_popcount(i);
            dp[i][1] = dp[i][0];
            if (i&(1<<(k-1))){
                for (pair<ll,ll> guyy:dp[i^(1<<(k-1))][0]){
                    if (dp[i][1].size() < 2) dp[i][1].push_back({zero,guyy.second});
                }
            }
        }
        for (int i=0;i<(1<<m);i++){
            dp[i][0] = dp[i][1];
        }
    }
    pair<pair<ll,ll>,pair<ll,ll>> dp2[1<<m][2];
    // max 0s
    for (int i=0;i<(1<<m);i++){
        dp2[i][0] = {{0,-1},{0,-1}};
        for (pair<ll,ll> nd:dp[i][0]){
            if (nd.first > dp2[i][0].first.first){
                if (nd.second != dp2[i][0].second.second) dp2[i][0] = {nd,dp2[i][0].first};
                else dp2[i][0].first = nd;
            }
            else if (nd.first > dp2[i][0].second.first){
                if (nd.second != dp2[i][0].first.second) dp2[i][0].second = nd;
            }
        }
    }
    for (int k=1;k<=m;k++){
        for (int i=0;i<(1<<m);i++){
            dp2[i][1] = dp2[i][0];
            if (!(i&(1<<(k-1)))){
                vector<pair<ll,ll>> temp = {dp2[i|(1<<(k-1))][0].first, dp2[i|(1<<(k-1))][0].second};
                for (pair<ll,ll> nd:temp){
                    if (nd.first > dp2[i][1].first.first){
                        if (nd.second != dp2[i][1].first.second) dp2[i][1] = {nd,dp2[i][1].first};
                        else dp2[i][1].first = nd;
                    }
                    else if (nd.first > dp2[i][1].second.first){
                        if (nd.second != dp2[i][1].first.second) dp2[i][1].second = nd;
                    }
                }
            }
        }
        for (int i=0;i<(1<<m);i++){
            dp2[i][0] = dp2[i][1];
        }
    }
    ll cutoff = n/2;
    for (int i=0;i<n;i++){
        ll useless = 0;
        ll cnt = 0;
        ll num = m;
        for (int j=0;j<m;j++){
            if (sum[j]-a[i][j] > cutoff){
                cnt++;
            }
            if (sum[j]-a[i][j] != cutoff){
                useless += (1<<j);
                num--;
            }
        }
        if (dp2[useless][0].first.second == i) cnt += dp2[useless][0].second.first;
        else cnt += dp2[useless][0].first.first;
        cout << cnt << "\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...