제출 #1316215

#제출 시각아이디문제언어결과실행 시간메모리
1316215sebokx중앙값 배열 (balkan11_medians)C++20
100 / 100
14 ms2204 KiB
#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define len(x) (int)x.size()
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int gen(int l, int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll gen(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
template<typename T, typename H>
ostream& operator<<(ostream& os, const pair<T, H> &p){
    return os << '(' << p.st << ", " << p.nd << ')';
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T> &vec){
    os << '{';
    for(int i = 0; i < len(vec); i++){
        if(i != 0) os << ", ";
        os << vec[i];
    }
    os << '}';
    return os;
}

const int N = 100'007;
int n;

int t[N];
bool blocked[N*2];
bool used[N*2];

void add_left(int &left){
    while(used[left] || blocked[left]) left++;
    cout << left << ' ';
    used[left++] = true;
}

void add_right(int &right){
    while(used[right] || blocked[right]) right--;
    cout << right << ' ';
    used[right--] = true;
}

void solve(){
    cin >> n;

    for(int i = 0; i < n; i++){
        cin >> t[i];
        blocked[t[i]] = true;
    } 

    cout << t[0] << ' ';
    used[t[0]] = true;

    int left = 1;
    int right = 2*n-1;

    for(int i = 1; i < n; i++){
        if(t[i] < t[i-1]){
            if(!used[t[i]]){
                cout << t[i] << ' ';
                used[t[i]] = true;
            }else add_left(left);
            add_left(left);
        }else if(t[i] > t[i-1]){
            if(!used[t[i]]){
                cout << t[i] << ' ';
                used[t[i]] = true;
            }else add_right(right);
            add_right(right);
        }else{
            add_left(left);
            add_right(right);
        }
    }

    cout << '\n';
}

int32_t main(){
    fastio();

    int tt = 1;
    // cin >> tt;
    while(tt--) solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...