Pair Programming CF 1547 C

void solve(){

    int k, n, m;
    cin >> k >> n >> m;

    vector<inta(n);
    vector<intb(m);

    for(int i=0;i<n;i++) cin >> a[i];
    for(int i=0;i<m;i++) cin >> b[i];

    int i=0, j=0;
    vector<int> ans;

    while(i < n || j < m){
        if(i<n && j<m){
            if(a[i] == 0) ans.push_back(a[i]), i++, k++;
            else if(b[j] == 0) ans.push_back(b[j]), j++, k++;
            else{
                if(a[i] < b[j] && a[i] <= k) ans.push_back(a[i]), i++;
                else if(b[j]<=a[i] && b[j] <=k)ans.push_back(b[j]), j++;
                else{
                    cout << "-1" << endl;
                    return;
                }
            }
        }
        else if(i<n){
            if(a[i] == 0) ans.push_back(a[i]), k++, i++;
            else if(a[i] <= k) ans.push_back(a[i]), i++;
            else{
                cout << "-1" << endl;
                return;
            }
        }
        else{
            if(b[j] == 0) ans.push_back(b[j]), k++, j++;
            else if(b[j] <= k) ans.push_back(b[j]), j++;
            else{
                cout << "-1" << endl;
                return;
            }
        }
    }

    // result
    for(auto val: ans) cout << val << " " ;
    cout << endl;
}

// take two pointer and analyze the situation. i.e. choose which 
// is best to take. Here taking new line is better then selecting 
// less line number is a better choice. ie. set priority order.

Comments

Popular Posts