Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.4 kB
7
Indexable
Never
        ll n;
        cin>>n;
        // 0 -> entry , 1 -> exit  
        vector<pair<ll,ll>> a(n);
        queue<pair<ll,ll>> q0 , q1;
        forsn(i,0,n) cin>>a[i].f;
        ll k ;
        cin>>k;
        for(ll i=0;i<n;i++){
            cin>>a[i].s;
            if(!a[i].s) q0.push({a[i].f,i});
            else q1.push({a[i].f,i});
        } 
        vector<ll> ans(n,0);
        ll curr = 0;
        ll dr = 1;  // dr = 1 means exit is in preference , else entry
        while((!q1.empty()) || (!q0.empty())){
            if(q1.empty()){
                ll time  = q0.front().f , i = q0.front().s;
                q0.pop();
                curr = max(curr,time);
                ans[i] = curr;
                curr++;
            }
            else if(q0.empty()){
                ll time  = q1.front().f , i = q1.front().s;
                q1.pop();
                curr = max(curr,time);
                ans[i] = curr;
                curr++;                
            }
            else{
                ll time0=q0.front().f , i = q0.front().s , time1= q1.front().f , j = q1.front().s;
                time0 = max(time0,curr) , time1 = max(time1,curr);          
                if(time0 == time1){             // checking if they reached gate at same time
                    if(time0-curr >= 1) dr = 1;     // checking if difference exceeds by 1 , then preference goes to exit
                     curr = max(curr,time0);
                    if(dr){
                        q1.pop();
                        ans[j] = curr;
                        curr++;
                    }
                    else{
                        q0.pop(),ans[i] = curr;
                        curr++;
                    }
                }
                else if(time1 < time0){ // else which comes first , goes first
                    curr = max(curr,time1);
                    ans[j] = curr;
                    curr++;
                    dr = 1;
                    q1.pop(); 
                }
                else{
                    curr = max(curr,time0);
                    q0.pop();
                    ans[i] = curr ;
                    curr++;
                    dr = 0; 
                }
            }
        }
        for(auto i : ans){
            cout<<i<<ln;
        }
Leave a Comment