#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 4e5+10;
int n, p[maxn], id[maxn], ans[maxn];
vector<set<int>> st;
void join(int id1, int id2) {
if(id1 == id2) return;
if(st[id1].size() < st[id2].size()) swap(id1,id2);
for(auto x : st[id2]) {
id[x] = id1;
st[id1].insert(x);
}
}
void solve() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> p[i];
id[p[i]] = i-1;
st.pb(set<int>{});
st.back().insert(p[i]);
}
for(int i = 1; i <= n; i++) {
int a = inf, b = inf, c = inf;
a = *st[id[p[i]]].begin();
if(2*i <= n) b = *st[id[p[2*i]]].begin();
if(2*i+1 <= n) c = *st[id[p[2*i+1]]].begin();
int mn = min({a,b,c});
st[id[mn]].erase(mn);
ans[i] = mn;
if(mn == c) {
// cout << p[i] << " " << p[2*i] << endl;
p[2*i+1] = p[2*i];
p[2*i] = p[i];
join(id[p[2*i]],id[p[2*i+1]]);
}
else if(mn == b) {
p[2*i] = p[i];
}
// cout << i << " - " << a << " " << b << " " << c << endl;
// for(int i = 1; i <= n; i++) {
// cout << p[i] << " ";
// }cout << endl << endl;
}
for(int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}cout << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |