Busy Intersection

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.9 kB
3
Indexable

int findAns(vector<int> arrival, vector<int> isSecondStreet){
    int n = arrival.size();
    queue<pair<int,int>> first,second;
    int currTime = 0;
    int prevTime = -1;
    bool isPrevFirstStreet = false;
    int currIndex = 0;

    vector<int> ans(n);
    while(currTime != arrival[n-1]+1){
        if((first.empty() && second.empty()) || currTime == arrival[currIndex]){
            if(isSecondStreet[currIndex]){
                second.push({currIndex, currTime});
            }else{
                first.push({currIndex, currTime});
            }
            currTime = arrival[currIndex];
            currIndex++;
        }else{
            if(first.empty() && !second.empty()){
                ans[second.front().first] = currTime;
                prevTime = currTime;
                second.pop();
                isPrevFirstStreet = false;
            }
            else if(!first.empty() && second.empty()){
                ans[first.front().first] = currTime;
                prevTime = currTime;
                first.pop();                
                isPrevFirstStreet = true;
            }
            else if(!first.empty() && !second.empty()){
                if(currTime - prevTime > 0){
                    ans[second.front().first] = currTime;
                    prevTime = currTime;
                    second.pop();
                }
                else{
                    if(isPrevFirstStreet){
                        ans[first.front().first] = currTime;
                        prevTime = currTime;
                        first.pop();
                    }
                    else {
                        ans[second.front().first] = currTime;
                        prevTime = currTime;
                        second.pop();
                    }
                }
            }
            currTime++;
        }
    }
    for(int i=0;i<n;i++){
        cout<<ans[i]<<" ";
    }cout<<"\n";
}