This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
int n, a[N], ans[N];
void solve(){
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
vector<pair<int, int>> q;
map<int, vector<int>> last;
for(int i = 1; i <= n; ++i){
if(q.empty()){
q.pb({a[i], 1});
last[a[i]].pb(i);
}else{
if(last[a[i]].empty()){
q.pb({a[i], 1});
last[a[i]].pb(i);
}else{
int x = last[a[i]].back(), s = 0;
if(x == i - 1){
q.back().second++;
last[a[i]].back() = i;
continue;
}
for(int cur = i - 1; ;){
cur -= q.back().second;
s += q.back().second;
last[q.back().first].pop_back();
q.pop_back();
if(cur == x){
auto v = q.back();
q.pop_back();
last[a[i]].pop_back();
last[a[i]].pb(i);
q.pb({v.first, v.second + s + 1});
break;
}
}
}
}
// for(auto p: q) cout << p.first << ' ' << p.second << '\n';
// cout << endl;
}
int x = 1;
for(auto p: q){
while(p.second--){
ans[x] = p.first;
++x;
}
// cout << p.first << ' ' << p.second << '\n';
}
for(int i = 1; i <= n; ++i) cout << ans[i] << '\n';
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
// en;
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:70:15: warning: unused variable 'aa' [-Wunused-variable]
70 | int tt = 1, aa;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |