#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int N = 200005;
struct com {
bool operator()(const pair<ll,ll>& a, const pair<ll,ll>& b) const {
if(a.second == b.second) return a.first < b.first;
return a.second < b.second;
}
};
set<pair<ll,ll>, com> st;
map<ll, int> in;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
for(int i = 1; i <= n; i++) {
ll x; cin >> x;
if(in.find(x) == in.end()) {
st.insert({x, i});
in[x] = i;
} else {
int target_idx = in[x];
while(!st.empty() && st.rbegin()->second > target_idx) {
in.erase(st.rbegin()->first);
st.erase(prev(st.end()));
}
}
}
st.insert({-1, (ll)n + 1});
int curr_print = 1;
for(auto it = st.begin(); it != prev(st.end()); ++it) {
while(curr_print < next(it)->second) {
cout << it->first << " ";
curr_print++;
}
}
cout << endl;
return 0;
}
/*
6
1
2
1
2
3
2
*/
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |