#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
int n;
int a[N];
map<int,bool> mp;
stack<int> st;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
st.push(-1);
for( int i=0;i<n;i++ )
{
cin >> a[i];
if( mp.find(a[i]) == mp.end() || mp[a[i]] == false ) mp[a[i]] = true;
else
{
while( !st.empty() && a[st.top()] != a[i] )
{
mp[a[st.top()]] = false;
st.pop();
}
st.pop();
}
st.push( i );
}
vector<int> ans;
while( !st.empty() )
{
int top = st.top();
st.pop();
if( top == -1 ) break;
int cnt = top;
while( cnt>st.top() )
{
ans.push_back( a[top] );
cnt--;
}
}
for( int i=n-1;i>=0;i-- ) cout << ans[i] << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |