#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,e) for (int i = s; i <= e; ++i)
#define rrep(i,s,e) for (int i = s; i >= e; --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(a) a.begin(), a.end()
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
const int mx = 2e5;
int a[2*mx+2];
int dfs2 (int u, int val, int d) {
int mi = min(val, min(a[2*u], a[2*u+1]));
if (mi==a[2*u+1]) {
if (val < a[2*u]) return min (dfs2(2*u, val, d+1), dfs2(2*u+1, val, d+1));
if (dfs2(2*u, a[2*u], d+1) <= dfs2 (2*u+1, a[2*u], d+1))
return dfs2 (2*u+1, val, d+1);
return dfs2 (2*u, val, d+1);
}
if (mi==a[2*u]) return dfs2 (2*u, val, d+1);
return d;
}
void dfs (int u) {
int mi = min(a[u], min(a[2*u], a[2*u+1]));
if (a[2*u+1]==mi) {
int mi2 = min(a[u], a[2*u]), ma = max(a[u], a[2*u]);
int l = dfs2 (2*u, mi2, 1), r = dfs2 (2*u+1, mi2, 1);
a[2*u] = mi2;
a[2*u+1] = ma;
if (l>r) swap (a[2*u], a[2*u+1]);
}
else if (a[2*u]==mi) a[2*u] = a[u];
a[u] = mi;
if (a[2*u]!=1e9) dfs (2*u);
if (a[2*u+1]!=1e9) dfs (2*u+1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
rep (i,1,2*mx+1) a[i] = 1e9;
int n; cin >> n;
rep (i,1,n) cin >> a[i];
dfs (1);
rep (i,1,n) cout << a[i] << " \n"[i==n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1868 KB |
Output is correct |
3 |
Correct |
1 ms |
1868 KB |
Output is correct |
4 |
Correct |
1 ms |
1868 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1868 KB |
Output is correct |
3 |
Correct |
1 ms |
1868 KB |
Output is correct |
4 |
Correct |
1 ms |
1868 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
1956 KB |
Output is correct |
7 |
Correct |
1 ms |
1868 KB |
Output is correct |
8 |
Correct |
1 ms |
1868 KB |
Output is correct |
9 |
Correct |
1 ms |
1868 KB |
Output is correct |
10 |
Correct |
2 ms |
1868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1868 KB |
Output is correct |
3 |
Correct |
1 ms |
1868 KB |
Output is correct |
4 |
Correct |
1 ms |
1868 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
1956 KB |
Output is correct |
7 |
Correct |
1 ms |
1868 KB |
Output is correct |
8 |
Correct |
1 ms |
1868 KB |
Output is correct |
9 |
Correct |
1 ms |
1868 KB |
Output is correct |
10 |
Correct |
2 ms |
1868 KB |
Output is correct |
11 |
Correct |
1 ms |
1868 KB |
Output is correct |
12 |
Correct |
1 ms |
1868 KB |
Output is correct |
13 |
Correct |
1 ms |
1868 KB |
Output is correct |
14 |
Correct |
2 ms |
1868 KB |
Output is correct |
15 |
Correct |
1 ms |
1868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1868 KB |
Output is correct |
3 |
Correct |
1 ms |
1868 KB |
Output is correct |
4 |
Correct |
1 ms |
1868 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
1956 KB |
Output is correct |
7 |
Correct |
1 ms |
1868 KB |
Output is correct |
8 |
Correct |
1 ms |
1868 KB |
Output is correct |
9 |
Correct |
1 ms |
1868 KB |
Output is correct |
10 |
Correct |
2 ms |
1868 KB |
Output is correct |
11 |
Correct |
1 ms |
1868 KB |
Output is correct |
12 |
Correct |
1 ms |
1868 KB |
Output is correct |
13 |
Correct |
1 ms |
1868 KB |
Output is correct |
14 |
Correct |
2 ms |
1868 KB |
Output is correct |
15 |
Correct |
1 ms |
1868 KB |
Output is correct |
16 |
Correct |
10 ms |
2140 KB |
Output is correct |
17 |
Correct |
9 ms |
2132 KB |
Output is correct |
18 |
Correct |
10 ms |
2124 KB |
Output is correct |
19 |
Correct |
96 ms |
2116 KB |
Output is correct |
20 |
Correct |
103 ms |
2120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1868 KB |
Output is correct |
3 |
Correct |
1 ms |
1868 KB |
Output is correct |
4 |
Correct |
1 ms |
1868 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
1956 KB |
Output is correct |
7 |
Correct |
1 ms |
1868 KB |
Output is correct |
8 |
Correct |
1 ms |
1868 KB |
Output is correct |
9 |
Correct |
1 ms |
1868 KB |
Output is correct |
10 |
Correct |
2 ms |
1868 KB |
Output is correct |
11 |
Correct |
1 ms |
1868 KB |
Output is correct |
12 |
Correct |
1 ms |
1868 KB |
Output is correct |
13 |
Correct |
1 ms |
1868 KB |
Output is correct |
14 |
Correct |
2 ms |
1868 KB |
Output is correct |
15 |
Correct |
1 ms |
1868 KB |
Output is correct |
16 |
Correct |
10 ms |
2140 KB |
Output is correct |
17 |
Correct |
9 ms |
2132 KB |
Output is correct |
18 |
Correct |
10 ms |
2124 KB |
Output is correct |
19 |
Correct |
96 ms |
2116 KB |
Output is correct |
20 |
Correct |
103 ms |
2120 KB |
Output is correct |
21 |
Correct |
38 ms |
3156 KB |
Output is correct |
22 |
Correct |
36 ms |
3152 KB |
Output is correct |
23 |
Correct |
35 ms |
3152 KB |
Output is correct |
24 |
Correct |
774 ms |
3148 KB |
Output is correct |
25 |
Correct |
775 ms |
3140 KB |
Output is correct |