Submission #896269

#TimeUsernameProblemLanguageResultExecution timeMemory
896269GrindMachineCouncil (JOI23_council)C++17
100 / 100
611 ms48328 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
edi

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

struct two_max{
    pii a,b;
    two_max(){
        a = b = {-1,-1};
    }

    void insert(pii p){
        if(p.ss == a.ss or p.ss == b.ss) return;
        if(p > a){
            b = a;
            a = p;
        }
        else{
            amax(b,p);
        }
    }
};

two_max merge(two_max tm1, two_max tm2){
    tm1.insert(tm2.a);
    tm1.insert(tm2.b);
    return tm1;
}

void solve(int test_case)
{
    int n,m; cin >> n >> m;
    vector<int> a(n);
    rep(i,n){
        rep(j,m){
            int x; cin >> x;
            a[i] = (a[i]<<1)|x;
        }
    }

    vector<int> ac(n); // complement of a[i]
    rep(i,n){
        ac[i] = ((1<<m)-1)^a[i];
    }

    vector<two_max> dp1(1<<m);
    rep(i,n){
        dp1[ac[i]].insert({1,i});
    }

    // pull from supermasks
    rep(bit,m){
        rep(mask,1<<m){
            if(mask&(1<<bit)) conts;
            dp1[mask] = merge(dp1[mask],dp1[mask^(1<<bit)]);
        }
    }

    vector<two_max> dp2(1<<m);
    rep(mask,1<<m){
        pii p1 = dp1[mask].a;
        pii p2 = dp1[mask].b;
        int sb = setbits(mask);

        if(p1.ff != -1){
            p1.ff = sb;
            dp2[mask].insert(p1); 
        }
        if(p2.ff != -1){
            p2.ff = sb;
            dp2[mask].insert(p2);
        }
    }

    // pull from submasks
    rep(bit,m){
        rep(mask,1<<m){
            if(mask&(1<<bit)){
                dp2[mask] = merge(dp2[mask],dp2[mask^(1<<bit)]);
            }
        }
    }

    vector<int> cnt(m);
    rep(i,n){
        rep(j,m){
            if(a[i]&(1<<j)){
                cnt[j]++;
            }
        }
    }

    // answer queries
    int want = n/2;

    rep(i,n){
        int mask = 0;
        int add = 0;

        rep(j,m){
            if(a[i]&(1<<j)){
                cnt[j]--;
            }
            if(cnt[j] == want){
                mask |= (1<<j);
            }
            else if(cnt[j] > want){
                add++;
            }
        }

        int mx = 0;
        if(dp2[mask].a.ss != i){
            amax(mx,dp2[mask].a.ff);
        }
        else{
            amax(mx,dp2[mask].b.ff);
        }

        int ans = add+mx;
        cout << ans << endl;
        
        rep(j,m){
            if(a[i]&(1<<j)){
                cnt[j]++;
            }
        }
    }
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    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...