#include<bits/stdc++.h>
using namespace std;
using ll = int;
map < ll, ll > A;
int main() {
ll n, m, sum,r, x, y, ans,i,p, j, lo, hi, lo1,hi1,cnt1,cnt, t,s, mx, mn, k, o;
cin >> n ;
stack < pair < ll, ll > > S;
for (i = 1; i <= n; i ++) {
cin >> x;
if ( A[x] == 0) {
S.push({x, 1});
A[x] ++;
}
else {
if ( S.empty()) {
S.push({x, 1});
A[x] ++;
continue;
}
sum = 0;
while(!S.empty() && S.top().first != x) {
A[S.top().first] -= (S.top().second);
sum += (S.top().second);
S.pop();
}
A[S.top().first] --;
sum += (S.top().second);
S.pop();
A[x] =sum + 1;
S.push({x, sum + 1});
}
}
vector <ll > v;
while ( !S.empty()) {
r = S.top().first;
s = S.top().second;
S.pop();
while ( s --) {
v.push_back(r);
}
}
reverse(v.begin(), v.end());
for (i = 0; i < v.size(); i ++) {
cout << v[i] << " ";
}
cout << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |