Submission #1276929

#TimeUsernameProblemLanguageResultExecution timeMemory
1276929dqamedians (balkan11_medians)C++20
65 / 100
2 ms576 KiB
#include <iostream>  
#include <vector>   
#include <queue>
#include <algorithm>
#include <numeric>
#include <map>
#include <sstream>
#include <cstring>
#include <stack>
#include <climits>

using namespace std;

const long long INF = 1e9;

int n;
int b[2005];
vector<int> a;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // freopen("cowtip.in", "r", stdin); 
    // freopen("cowtip.out", "w", stdout);

    int n; cin >> n;
    bool used[2005];
    memset(used, false, sizeof(used));
    for (int i = 1; i <= n; i++) {
        cin >> b[i];
    }

    int m = 2*n - 1;
    int num = 1;

    for (int i = 1; i <= n; i++) {
        bool check = false;
        int higher = 0, lower = 0;
        int elements = 2 * i - 1;
        for(int x : a) {
            if (x > b[i]) {
                higher++;
            } else if (x < b[i]) {
                lower++;
            }
        }
        if(!used[b[i]]) {
            a.push_back(b[i]);
            used[b[i]] = true;
        }
        while((int)a.size() != elements) {
            if(lower > higher) {
                while(m > 0 && used[m]) {
                    m--;
                }
                if (m <= 0) break;
                a.push_back(m);
                used[m] = true;
                higher++;
            } else if (lower < higher) {
                while(num <= m && used[num]) {
                    num++;
                }
                if (num > 2*n - 1) break;
                a.push_back(num);
                used[num] = true;
                lower++;
            } else if (lower == higher) {
                while(m > 0 && used[m]) {
                    m--;
                }
                if (m <= 0) break;
                a.push_back(m);
                used[m] = true;
                higher++;
            
                while(num <= m && used[num]) {
                    num++;
                }
                if (num > 2*n - 1) break;
                a.push_back(num);
                used[num] = true;
                lower++;
            }
        }
    }
    for (auto v : a) {
            cout << v << " ";
        }
        cout << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...