1759G - Restore the Permutation - CodeForces Solution


binary search constructive algorithms data structures greedy math *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define el '\n'
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define vi vector<int>
#define vl vector<ll>
#define popcount __builtin_popcountll
#define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int mod = 1e9 + 7;
const ll INF = 1e17 + 5;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l , ll r) {
    return uniform_int_distribution<ll>(l, r)(rng);
}



int main(){
    ios;
    int t;cin >> t;
    while(t--){
        int n;cin >> n;
        vector<int> arr(n / 2);
        for(auto &x:arr)cin >> x;
        vector<int> res(n + 1);
        map<int,int> mp;
        for(int i = 1;i <= n / 2;i++){
            mp[arr[i - 1]] = 2 * i;
            res[2 * i] = arr[i - 1];
        }
        set<int> s;
        bool ok = true;
        for(int i = n;i >= 1;i--){
            if(!mp[i]){
                if(s.empty()){
                    ok = false;
                    break;
                }else{
                    const int idx = *s.rbegin();
                    res[idx - 1] = i;
                    s.erase(idx); 
                }
            }else{
                s.insert(mp[i]);
            }
        }
        if(!ok){
            cout << -1 << el;
        }else{
            for(int i = 1;i <= n;i++){
                cout << res[i] << " ";
            }
            cout << el;
        }
    }
}


Comments

Submit
0 Comments
More Questions

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
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments