#include <bits/stdc++.h>
#define int long long
using namespace std;
const long long N = 2e5 + 5;
pair<int, int> a[N];
int last[N];
int idx[N];
void solve()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i].first;
a[i].second = i;
}
vector<pair<int, int>> stak;
for (int i = 1; i <= n; i++)
{
if (last[a[i].first] != 0)
{
while (stak.size())
{
pair<int, int> sp = stak.back();
if (sp.second <= last[a[i].first])
{
break;
}
else
{
last[sp.first] = idx[sp.second];
// cout << "lastof " << sp.first << " assigned to " << idx[sp.second] << endl;
stak.pop_back();
}
}
}
stak.push_back(a[i]);
idx[i] = last[a[i].first];
last[a[i].first] = i;
// for (int i = 1; i <= n; i++)
// {
// cerr << idx[i] << " ";
// }
// cerr << endl;
// for (int i = 1; i <= n; i++)
// {
// cerr << last[i] << " ";
// }
// cerr << endl;
}
// vector<int> ans(n+1);
int j = 0;
for (int i = 1; i <= n; i++)
{
while (j < stak.size() - 1 and stak[j].second < i)
{
j++;
}
cout << stak[j].first << " ";
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++)
{
// cout << "Case #" << i << ':' << ' ';
solve();
cout << endl;
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |