#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];
unordered_map<int,int> dp[mx+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];
rrep (i,n,1) {
vi aff;
aff.pb(a[i]);
int p = i/2;
while (p) {
if (aff.back()==a[2*p+1]) aff.pb(a[2*p]);
aff.pb(a[p]);
p = p/2;
}
for (int x : aff) {
int mi = min(x, min(a[i*2], a[2*i+1]));
if (x==mi) dp[i][x] = 1;
else if (a[i*2]==mi) dp[i][x] = dp[2*i][x]+1;
else {
if (a[2*i]<x) {
if (dp[2*i][ a[2*i]] <= dp[2*i+1][ a[2*i]]) dp[i][x] = dp[2*i+1][x]+1;
else dp[i][x] = dp[2*i][x]+1;
}
else dp[i][x] = min(dp[2*i][x], dp[2*i+1][x])+1;
}
}
}
rep (i,1,n) {
int mi = min(a[i], min(a[2*i], a[2*i+1]));
if (a[2*i+1]==mi) {
int mi2 = min(a[i], a[2*i]), ma = max(a[i], a[2*i]);
a[2*i] = mi2;
a[2*i+1] = ma;
if (dp[2*i][mi2]>dp[2*i+1][mi2]) swap (a[2*i], a[2*i+1]);
}
else if (a[2*i]==mi) a[2*i] = a[i];
a[i] = mi;
}
rep (i,1,n) cout << a[i] << " \n"[i==n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12748 KB |
Output is correct |
2 |
Correct |
7 ms |
12836 KB |
Output is correct |
3 |
Correct |
8 ms |
12800 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12748 KB |
Output is correct |
2 |
Correct |
7 ms |
12836 KB |
Output is correct |
3 |
Correct |
8 ms |
12800 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12748 KB |
Output is correct |
6 |
Correct |
7 ms |
12748 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
8 |
Correct |
7 ms |
12748 KB |
Output is correct |
9 |
Correct |
7 ms |
12748 KB |
Output is correct |
10 |
Correct |
7 ms |
12748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12748 KB |
Output is correct |
2 |
Correct |
7 ms |
12836 KB |
Output is correct |
3 |
Correct |
8 ms |
12800 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12748 KB |
Output is correct |
6 |
Correct |
7 ms |
12748 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
8 |
Correct |
7 ms |
12748 KB |
Output is correct |
9 |
Correct |
7 ms |
12748 KB |
Output is correct |
10 |
Correct |
7 ms |
12748 KB |
Output is correct |
11 |
Correct |
9 ms |
13388 KB |
Output is correct |
12 |
Correct |
9 ms |
13388 KB |
Output is correct |
13 |
Correct |
8 ms |
13324 KB |
Output is correct |
14 |
Correct |
9 ms |
13388 KB |
Output is correct |
15 |
Correct |
10 ms |
13388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12748 KB |
Output is correct |
2 |
Correct |
7 ms |
12836 KB |
Output is correct |
3 |
Correct |
8 ms |
12800 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12748 KB |
Output is correct |
6 |
Correct |
7 ms |
12748 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
8 |
Correct |
7 ms |
12748 KB |
Output is correct |
9 |
Correct |
7 ms |
12748 KB |
Output is correct |
10 |
Correct |
7 ms |
12748 KB |
Output is correct |
11 |
Correct |
9 ms |
13388 KB |
Output is correct |
12 |
Correct |
9 ms |
13388 KB |
Output is correct |
13 |
Correct |
8 ms |
13324 KB |
Output is correct |
14 |
Correct |
9 ms |
13388 KB |
Output is correct |
15 |
Correct |
10 ms |
13388 KB |
Output is correct |
16 |
Correct |
152 ms |
58108 KB |
Output is correct |
17 |
Correct |
157 ms |
58128 KB |
Output is correct |
18 |
Correct |
159 ms |
58116 KB |
Output is correct |
19 |
Correct |
158 ms |
58188 KB |
Output is correct |
20 |
Correct |
155 ms |
58116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12748 KB |
Output is correct |
2 |
Correct |
7 ms |
12836 KB |
Output is correct |
3 |
Correct |
8 ms |
12800 KB |
Output is correct |
4 |
Correct |
7 ms |
12748 KB |
Output is correct |
5 |
Correct |
7 ms |
12748 KB |
Output is correct |
6 |
Correct |
7 ms |
12748 KB |
Output is correct |
7 |
Correct |
7 ms |
12748 KB |
Output is correct |
8 |
Correct |
7 ms |
12748 KB |
Output is correct |
9 |
Correct |
7 ms |
12748 KB |
Output is correct |
10 |
Correct |
7 ms |
12748 KB |
Output is correct |
11 |
Correct |
9 ms |
13388 KB |
Output is correct |
12 |
Correct |
9 ms |
13388 KB |
Output is correct |
13 |
Correct |
8 ms |
13324 KB |
Output is correct |
14 |
Correct |
9 ms |
13388 KB |
Output is correct |
15 |
Correct |
10 ms |
13388 KB |
Output is correct |
16 |
Correct |
152 ms |
58108 KB |
Output is correct |
17 |
Correct |
157 ms |
58128 KB |
Output is correct |
18 |
Correct |
159 ms |
58116 KB |
Output is correct |
19 |
Correct |
158 ms |
58188 KB |
Output is correct |
20 |
Correct |
155 ms |
58116 KB |
Output is correct |
21 |
Correct |
681 ms |
214172 KB |
Output is correct |
22 |
Correct |
673 ms |
215364 KB |
Output is correct |
23 |
Correct |
670 ms |
215492 KB |
Output is correct |
24 |
Correct |
690 ms |
215380 KB |
Output is correct |
25 |
Correct |
689 ms |
215368 KB |
Output is correct |