Submission #872785

#TimeUsernameProblemLanguageResultExecution timeMemory
872785PagodePaivaStone Arranging 2 (JOI23_ho_t1)C++17
100 / 100
173 ms16204 KiB
#include<bits/stdc++.h>

using namespace std;

int main(){
    int n;
    cin >> n;
    stack <pair <int,int>> s;
    vector <int> res;
    set <int> aux;

    for(int i = 0;i < n;i++){
        int x;
        cin >> x;
        // cout << x << ' ';
        if(aux.find(x) != aux.end()){
            while(!s.empty()){
                pair <int, int> t = s.top();
                s.pop();
                aux.erase(t.first);
                if(t.first == x){
                    // s.push({x, i});
                    break;
                }
            }
        }
        s.push({x, i});
        aux.insert(x);
    }

    int cl;

    for(int i = n-1;i >= 0;i--){
        if(s.empty()) s.push({-1, -1});
        pair <int, int> t = s.top();
        if(t.second == i){
            cl = t.first;
            s.pop();
        }
        res.push_back(cl);
    }

    reverse(res.begin(), res.end());

    for(auto x : res){
        cout << x << ' ';
    }

    cout << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...