This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) int32_t(s.length())
#define MASK(k)(1LL<<(k))
#define TASK "test"
using namespace std;
typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;
const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MX = 200005;
int n;
int a[MX];
int ans[MX];
stack<int> pos[MX];
vector<int> val;
void nhap(){
cin >> n;
val.resize(n);
for(int i=1; i<=n; i++) cin >> a[i], val[i-1] = a[i];
sort(val.begin(), val.end());
val.erase(unique(val.begin(), val.end()), val.end());
for(int i=1; i<=n; i++) a[i] = lower_bound(val.begin(), val.end(), a[i]) - val.begin() + 1;
}
void solve(){
stack<int> cur;
for(int i=1; i<=n; i++){
if(pos[a[i]].size() == 0){
pos[a[i]].push(i);
cur.push(i);
continue;
}
while(cur.size() && cur.top() > pos[a[i]].top()){
pos[a[cur.top()]].pop();
cur.pop();
}
cur.push(i);
pos[a[i]].push(i);
}
while(cur.size()) ans[cur.top()] = a[cur.top()], cur.pop();
for(int i=1, vl=0; i<=n; i++){
if(ans[i]) vl = val[ans[i]-1];
cout << vl << '\n';
}
}
int32_t main(){
fastIO;
//file(TASK);
nhap();
solve();
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... |