Submission #796066

#TimeUsernameProblemLanguageResultExecution timeMemory
796066vjudge1Council (JOI23_council)C++17
100 / 100
2279 ms90476 KiB


// Author : حسن

#include <bits/stdc++.h>



using namespace std;

#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"


const int N = 3e5 + 9 , mod = 1e9 + 7;
ll c[N] = {} , d[N] = {} , a[N][22] = {}, b[N] = {}  , us[N] , dp[N * 4][4] = {};


void solve(){
    ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 0;
    cin>>n>>m;
    for(i = 0; i <= (1 << 20); i++)
        dp[i][0] = dp[i][1] = dp[i][2] = dp[i][3] = 0 , dp[i][1] = -1;
    for(i = 1;i  <= n; i++)
        for(j = 0; j < m; j++){
            cin>>a[i][j] , b[j] += a[i][j];
            if(a[i][j])
                d[i] += a[i][j] , c[i] += (1 << j);
    }
    y = n / 2;
    for(i = 1; i <= n; i++){
        x = c[i] , c[i] = ((1 << m) - 1) ^ c[i];
        if(dp[c[i]][1] == -1)
            dp[c[i]][1] = i , dp[c[i]][0] = m - d[i];
        else
            dp[c[i]][3] = i , dp[c[i]][2] =  m - d[i];
        c[i] = x;
        }
        for(i = (1 << m) - 1; i >= 0; i--){
            for(j = 0; j < m; j++){
                if(i & (1 << j)){
                    x = i ^ (1 << j);
                    dp[i][0]--;
                    dp[i][2]--;
                    vector<pair<ll,ll>>v;
                    v.pb({dp[i][0] , dp[i][1]});
                    v.pb({dp[i][2] , dp[i][3]});
                    v.pb({dp[x][0] , dp[x][1]});
                    v.pb({dp[x][2] , dp[x][3]});
                    sort(rall(v));
                    dp[x][0] = v[0].fi , dp[x][1] = v[0].se;
                    for(auto to : v)
                    if(to.se != dp[x][1]){
                        dp[x][2] = to.fi , dp[x][3] = to.se;
                        break;
                    }
                dp[i][0]++;
                dp[i][2]++;
                }
            }
        }
    for(i = 0; i < (1<<m); i++){
        for(j = 0; j < m; j++){
            if(!((1 << j) & i)){
                x = i ^ (1 << j);
                vector<pair<ll,ll>>v;
                v.pb({dp[i][0] , dp[i][1]});
                v.pb({dp[i][2] , dp[i][3]});
                v.pb({dp[x][0] , dp[x][1]});
                v.pb({dp[x][2] , dp[x][3]});
                sort(rall(v));
                dp[x][0] = v[0].fi , dp[x][1] = v[0].se;
                for(auto to : v)
                if(to.se != dp[x][1]){
                    dp[x][2] = to.fi , dp[x][3] = to.se;
                    break;
                }
            }
        }
    }
    for(j = 0; j < m; j++)
        s += (b[j] >= y);
     for(i = 1; i <= n; i++){
            mx = 0;
            for(j = 0; j < m; j++){
                b[j] -= a[i][j];
                if(a[i][j] == 1 && b[j] == y - 1)
                    s--;
            }
            f =x = 0;
            for(j = 0; j < m; j++){
                if(b[j] == y) x += (1 << j);
                else if(b[j] > y)   f++;
            }
            k = dp[x][0];
            if(dp[x][1] == i)
                k = dp[x][2];
            //cout<<s<<" ";
            cout<<f + k<<"\n";
            for(j = 0; j < m; j++){
                        b[j] += a[i][j];
                if(a[i][j] == 1 && b[j] == y)
                    s++;
                }
        }
}

int main(){

     TL;
     /*
     #ifndef ONLINE_JUDGE
     freopen("input.txt", "r", stdin);
     freopen("output.txt", "w", stdout);
     #endif
     */

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

while(t--)
     {
     solve();
     }

}
// Author : حسن

Compilation message (stderr)

council.cpp: In function 'void solve()':
council.cpp:32:8: warning: unused variable 'q' [-Wunused-variable]
   32 |     ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |        ^
council.cpp:32:28: warning: unused variable 'z' [-Wunused-variable]
   32 |     ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                            ^
council.cpp:32:43: warning: unused variable 'l' [-Wunused-variable]
   32 |     ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                                           ^
council.cpp:32:47: warning: unused variable 'r' [-Wunused-variable]
   32 |     ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                                               ^
council.cpp:32:63: warning: unused variable 'mn' [-Wunused-variable]
   32 |     ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 0;
      |                                                               ^~
council.cpp:32:76: warning: variable 'mx' set but not used [-Wunused-but-set-variable]
   32 |     ll q , i , j , m , n , z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = 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...