#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 main() {
ios::sync_with_stdio(0);
cin.tie(0);
int a[2*mx+1];
map<ii, int> dp;
rep (i,1,2*mx) 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 |
1 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1872 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 |
1872 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 |
1868 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 |
1 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1872 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 |
1868 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 |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
5 ms |
2636 KB |
Output is correct |
12 |
Correct |
5 ms |
2636 KB |
Output is correct |
13 |
Correct |
4 ms |
2636 KB |
Output is correct |
14 |
Correct |
5 ms |
2636 KB |
Output is correct |
15 |
Correct |
4 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1872 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 |
1868 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 |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
5 ms |
2636 KB |
Output is correct |
12 |
Correct |
5 ms |
2636 KB |
Output is correct |
13 |
Correct |
4 ms |
2636 KB |
Output is correct |
14 |
Correct |
5 ms |
2636 KB |
Output is correct |
15 |
Correct |
4 ms |
2644 KB |
Output is correct |
16 |
Correct |
303 ms |
69280 KB |
Output is correct |
17 |
Correct |
306 ms |
69152 KB |
Output is correct |
18 |
Correct |
316 ms |
69432 KB |
Output is correct |
19 |
Correct |
331 ms |
69216 KB |
Output is correct |
20 |
Correct |
329 ms |
69204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1872 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 |
1868 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 |
1 ms |
1876 KB |
Output is correct |
11 |
Correct |
5 ms |
2636 KB |
Output is correct |
12 |
Correct |
5 ms |
2636 KB |
Output is correct |
13 |
Correct |
4 ms |
2636 KB |
Output is correct |
14 |
Correct |
5 ms |
2636 KB |
Output is correct |
15 |
Correct |
4 ms |
2644 KB |
Output is correct |
16 |
Correct |
303 ms |
69280 KB |
Output is correct |
17 |
Correct |
306 ms |
69152 KB |
Output is correct |
18 |
Correct |
316 ms |
69432 KB |
Output is correct |
19 |
Correct |
331 ms |
69216 KB |
Output is correct |
20 |
Correct |
329 ms |
69204 KB |
Output is correct |
21 |
Runtime error |
996 ms |
262148 KB |
Execution killed with signal 9 |
22 |
Halted |
0 ms |
0 KB |
- |