/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
// lethal option
#include<bits/stdc++.h>
using namespace std;
#define all(flg) flg.begin(), flg.end()
#define pb push_back
#define fi first
#define se second
#define eb emplace_back
#define ii pair<int, int>
#define vi vector<int>
#define PI 3.141592653589793238462643383279502884
#define ll long long
#define ld long double
#define for1(i, ff, gg) for(int i = ff; i <= gg; ++i)
#define for2(i, ff, gg) for(int i = ff; i >= gg; --i)
const int mod = 1e9 + 7;
const int maxN = 4e5 + 5;
int n, a[maxN];
int x, y, z, k;
int cnt = n;
vi vc[maxN];
bool used[maxN];
int calc(int node, bool fucked = 0){
if(node <= n){
assert(node > 0);
if(used[node]) return mod;
else return node;
}
int res = mod;
for(int cc : vc[node]){
int val = calc(cc, fucked);
res = min(res, val);
}
return res;
}
int ans[maxN];
signed main(){
// freopen(".inp", "r", stdin);
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for1(i, 1, n) cin >> a[i];
cnt = n;
memset(used, 0, sizeof(used));
for1(i, 1, n){
repas:
// for1(j, i, n) cout << a[j] << " ";
// cout << endl;
int vl = calc(a[i]);
int semi = mod;
int left = i * 2;
int right = i * 2 + 1;
if(left <= n) semi = min(semi, calc(a[left]));
if(right <= n) semi = min(semi, calc(a[right]));
if(left <= n && calc(a[left]) < vl){
swap(a[left], a[i]); semi = mod;
}
if(semi >= vl){
vl = calc(a[i]);
ans[i] = vl; used[vl] = 1;
continue;
}
if(right <= n){
int node = ++cnt;
vc[node].pb(a[left]);
vc[node].pb(a[i]);
ans[i] = a[right];
used[a[right]] = 1;
a[left] = node;
a[right] = node;
}
}
for1(i, 1, n) cout << ans[i] << " ";
cout << endl;
}
Compilation message
swap.cpp: In function 'int main()':
swap.cpp:59:9: warning: label 'repas' defined but not used [-Wunused-label]
59 | repas:
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
10068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
10068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
10068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
10068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
10068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |