1903B - StORage room - CodeForces Solution


bitmasks greedy

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000009
#define int long long
#define pb push_back
#define fi first
#define se second
#define pii pair<int,int>
//bigmod
int bigmod(int b,int p){
    if(p==0)return 1;
    if(p==1)return b;
    int a=bigmod(b,p/2);
    a*=a;
    a%=MOD;
    if(p%2==1)a*=b;
    a%=MOD;
    return a;
}
 
//fibonacci sequence
int fib[1];
void fib_init(){
    fib[1]=fib[2]=1;
    for(int i=3;i<=300005;i++){
        fib[i]=fib[i-1]+fib[i-2];
        fib[i]%=MOD;
    }
}
int sumfib(int a,int b){
    int fsum=fib[b+2]-1;
    int ssum=fib[a+1]-1;
    int ans=fsum-ssum;
    ans%=MOD;
    return ans;
}
 
//segment tree with lazy
struct segtree{
    struct node{
        int max=0;
    };
    struct lazynode{
        int sum=0;
    };
    vector<node> tree;
    vector<lazynode> lazy;
    vector<int> arr;
    int n;
    void build(int ind, int l, int r){
        if(l==r){
            tree[ind].max=arr[l];
            lazy[ind].sum=0;
            return;
        }
        int mid =l+(r-l)/2;
        build(ind*2,l,mid);
        build(ind*2+1,mid+1,r);
        tree[ind].max=max(tree[ind*2].max,tree[ind*2+1].max);
        lazy[ind].sum=0;
        return;
    }
 
    segtree(vector<int>& a) : n(a.size()) {
        int SIZE = n;
        tree.resize(4 * SIZE );
        lazy.resize(4 * SIZE );
        arr=a;
        build(1, 0, n-1);
    }
 
    void update(int ind,int l,int r, int li,int ri,int x){
        int mid=l+(r-l)/2;
        if(li>r||ri<l)return;
        if(l==r){
            tree[ind].max+=lazy[ind].sum+x;
            lazy[ind].sum=0;
        }
        if(lazy[ind].sum!=0){
            tree[ind].max+=lazy[ind].sum;
            lazy[ind*2].sum+=lazy[ind].sum;
            lazy[ind*2+1].sum+=lazy[ind].sum;
            lazy[ind].sum=0;
        }
        if(li<=l&&r<=ri){
            tree[ind].max+=x;
            lazy[ind*2+1].sum+=x;
            lazy[ind*2].sum+=x;
            return;
        }
        update(ind*2,l,mid,li,ri,x);
        update(ind*2+1,mid+1,r,li,ri,x);
        tree[ind].max=max(tree[ind*2].max,tree[ind*2+1].max);
        return;
    }
 
    node query(int ind,int l,int r, int li,int ri){
        node nul;
        nul.max=INT_MIN;
        int mid=l+(r-l)/2;
        if(li>r||ri<l){
            return nul;
        }
        if(l==r){
            tree[ind].max+=lazy[ind].sum;
            lazy[ind].sum=0;
            return tree[ind];
        }
        if(lazy[ind].sum!=0){
            tree[ind].max+=lazy[ind].sum;
            lazy[ind*2].sum+=lazy[ind].sum;
            lazy[ind*2+1].sum+=lazy[ind].sum;
            lazy[ind].sum=0;
        }
        if(li<=l&&r<=ri){
            return tree[ind];
        }
        node a1=query(ind*2,l,mid,li,ri);
        node a2=query(ind*2+1,mid+1,r,li,ri);
        a1.max=max(a1.max,a2.max);
        return a1;
    }
 
    void update(int li,int ri,int x){
        update(1,0,n-1,li,ri,x);
    }
    int query(int li,int ri){
        return query(1,0,n-1,li,ri).max;
    }
};
 
//merge sort tree
struct mstree{
    
    struct node{
        vector<int> nums;
    };
    vector<node> tree;
    vector<int> arr;
    int n;
    void build(int ind, int l, int r){
        if(l==r){
            tree[ind].nums={arr[l]};
            return;
        }
        int mid =l+(r-l)/2;
        build(ind*2,l,mid);
        build(ind*2+1,mid+1,r);
        merge(tree[2*ind].nums.begin(),tree[2*ind].nums.end(),tree[2*ind+1].nums.begin(),tree[2*ind+1].nums.end(),back_inserter(tree[ind].nums));
        return;
    }
    
    mstree(vector<int>& a) : n(a.size()) {
        int SIZE = n;
        tree.resize(4 * SIZE );
        arr=a;
        build(1, 0, n-1);
    }
    int query(int ind,int l,int r,int li,int ri,int ml,int mr){
        int mid=l+(r-l)/2;
        if(li>r||ri<l){
            return 0;
        }
        if(li<=l&&r<=ri){
            return lower_bound(tree[ind].nums.begin(),tree[ind].nums.end(),mr)-lower_bound(tree[ind].nums.begin(),tree[ind].nums.end(),ml);
        }
        int a1=query(ind*2,l,mid,li,ri,ml,mr);
        int a2=query(ind*2+1,mid+1,r,li,ri,ml,mr);
        return a1+a2;
    }
    int query(int l,int r,int ml,int mr){
        return query(1,0,n-1,l,r,ml,mr);
    }
};
 
vector<int> adj[10];
int dfs(int a,int prev){
    for(int i=0;i<adj[a].size();i++){
        if(adj[a][i]!=prev)dfs(a,i);
    }
    return 0;
}
 
 
void solve(){
    int n;
    cin>>n;
    int arr[n][n];
    for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>arr[i][j];
    int fin[n];
    for(int i=0;i<n;i++)fin[i]=0;
    bool contr=0;
    for(int bit=1;bit<1073741824;bit*=2){
        int cur[n][n];
        for(int i=0;i<n;i++)for(int j=0;j<n;j++)cur[i][j]=(arr[i][j]&bit);
        int ans[n];
        for(int i=0;i<n;i++)ans[i]=1;
        for(int i=0;i<n;i++)for(int j=0;j<n;j++){
            if(i==j)continue;
            if(cur[i][j]==0)ans[i]=0,ans[j]=0;
        }
        for(int i=0;i<n;i++)for(int j=0;j<n;j++){
            if(i==j)continue;
            if(cur[i][j]>=1&&ans[i]==0&&ans[j]==0){
                cout<<"NO\n";
                return;
            }
        }
        // cout<<'\n';
        for(int i=0;i<n;i++)fin[i]+=ans[i]*bit;
    }
    cout<<"YES\n";
    for(int i=0;i<n;i++)cout<<fin[i]<<' ';
    cout<<'\n';
    return;
}
 
 
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)solve();
}


Comments

Submit
0 Comments
More Questions

1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes