#include <bits/stdc++.h>
#define X first
#define Y second
#define MP make_pair
#define ll long long
using namespace std;
const int N = 5e5 + 12;
const int mod = 1e9 + 7;
int n, p[N];
map< pair<int, int>, int > dp;
int get(int v, int val){
if(dp.count(MP(v, val)))
return dp[MP(v, val)];
if(val < min(p[v * 2], p[v * 2 + 1]))
return v;
if(p[v * 2] < p[v * 2 + 1])
return dp[MP(v, val)] = get(v * 2, val);
int mn = min(p[v * 2], val), mx = max(p[v * 2], mx);
if(get(v * 2, mn) < get(v * 2 + 1, mn)){
if(mn == val)
return dp[MP(v, val)] = get(v * 2, val);
else
return dp[MP(v, val)] = get(v * 2 + 1, val);
}
else{
if(mn != val)
return dp[MP(v, val)] = get(v * 2, val);
else
return dp[MP(v, val)] = get(v * 2 + 1, val);
}
}
int main () {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
memset(p, 0x3f, sizeof(p));
cin >> n;
for(int i = 1;i <= n;i++)
cin >> p[i];
for(int i = 1;i <= n;i++){
if(p[i * 2 + 1] < min(p[i], p[i * 2])){
int mn = min(p[i], p[i * 2]), mx = max(p[i], p[i * 2]);
p[i] = p[i * 2 + 1], p[i * 2] = mx, p[i * 2 + 1] = mn;
if(get(i * 2, mn) < get(i * 2 + 1, mn))
p[i * 2] = mn, p[i * 2 + 1] = mx;
}
else if(p[i * 2] < p[i]){
swap(p[i], p[i * 2]);
}
cout << p[i] << " ";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2284 KB |
Output is correct |
2 |
Correct |
2 ms |
2284 KB |
Output is correct |
3 |
Correct |
2 ms |
2284 KB |
Output is correct |
4 |
Correct |
2 ms |
2284 KB |
Output is correct |
5 |
Correct |
2 ms |
2284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2284 KB |
Output is correct |
2 |
Correct |
2 ms |
2284 KB |
Output is correct |
3 |
Correct |
2 ms |
2284 KB |
Output is correct |
4 |
Correct |
2 ms |
2284 KB |
Output is correct |
5 |
Correct |
2 ms |
2284 KB |
Output is correct |
6 |
Correct |
2 ms |
2284 KB |
Output is correct |
7 |
Correct |
2 ms |
2284 KB |
Output is correct |
8 |
Correct |
3 ms |
2284 KB |
Output is correct |
9 |
Correct |
2 ms |
2284 KB |
Output is correct |
10 |
Correct |
2 ms |
2284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2284 KB |
Output is correct |
2 |
Correct |
2 ms |
2284 KB |
Output is correct |
3 |
Correct |
2 ms |
2284 KB |
Output is correct |
4 |
Correct |
2 ms |
2284 KB |
Output is correct |
5 |
Correct |
2 ms |
2284 KB |
Output is correct |
6 |
Correct |
2 ms |
2284 KB |
Output is correct |
7 |
Correct |
2 ms |
2284 KB |
Output is correct |
8 |
Correct |
3 ms |
2284 KB |
Output is correct |
9 |
Correct |
2 ms |
2284 KB |
Output is correct |
10 |
Correct |
2 ms |
2284 KB |
Output is correct |
11 |
Correct |
2 ms |
2284 KB |
Output is correct |
12 |
Correct |
2 ms |
2284 KB |
Output is correct |
13 |
Correct |
3 ms |
2284 KB |
Output is correct |
14 |
Correct |
2 ms |
2412 KB |
Output is correct |
15 |
Correct |
2 ms |
2412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2284 KB |
Output is correct |
2 |
Correct |
2 ms |
2284 KB |
Output is correct |
3 |
Correct |
2 ms |
2284 KB |
Output is correct |
4 |
Correct |
2 ms |
2284 KB |
Output is correct |
5 |
Correct |
2 ms |
2284 KB |
Output is correct |
6 |
Correct |
2 ms |
2284 KB |
Output is correct |
7 |
Correct |
2 ms |
2284 KB |
Output is correct |
8 |
Correct |
3 ms |
2284 KB |
Output is correct |
9 |
Correct |
2 ms |
2284 KB |
Output is correct |
10 |
Correct |
2 ms |
2284 KB |
Output is correct |
11 |
Correct |
2 ms |
2284 KB |
Output is correct |
12 |
Correct |
2 ms |
2284 KB |
Output is correct |
13 |
Correct |
3 ms |
2284 KB |
Output is correct |
14 |
Correct |
2 ms |
2412 KB |
Output is correct |
15 |
Correct |
2 ms |
2412 KB |
Output is correct |
16 |
Correct |
18 ms |
3564 KB |
Output is correct |
17 |
Correct |
18 ms |
3540 KB |
Output is correct |
18 |
Correct |
18 ms |
3564 KB |
Output is correct |
19 |
Correct |
37 ms |
5740 KB |
Output is correct |
20 |
Correct |
40 ms |
5740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2284 KB |
Output is correct |
2 |
Correct |
2 ms |
2284 KB |
Output is correct |
3 |
Correct |
2 ms |
2284 KB |
Output is correct |
4 |
Correct |
2 ms |
2284 KB |
Output is correct |
5 |
Correct |
2 ms |
2284 KB |
Output is correct |
6 |
Correct |
2 ms |
2284 KB |
Output is correct |
7 |
Correct |
2 ms |
2284 KB |
Output is correct |
8 |
Correct |
3 ms |
2284 KB |
Output is correct |
9 |
Correct |
2 ms |
2284 KB |
Output is correct |
10 |
Correct |
2 ms |
2284 KB |
Output is correct |
11 |
Correct |
2 ms |
2284 KB |
Output is correct |
12 |
Correct |
2 ms |
2284 KB |
Output is correct |
13 |
Correct |
3 ms |
2284 KB |
Output is correct |
14 |
Correct |
2 ms |
2412 KB |
Output is correct |
15 |
Correct |
2 ms |
2412 KB |
Output is correct |
16 |
Correct |
18 ms |
3564 KB |
Output is correct |
17 |
Correct |
18 ms |
3540 KB |
Output is correct |
18 |
Correct |
18 ms |
3564 KB |
Output is correct |
19 |
Correct |
37 ms |
5740 KB |
Output is correct |
20 |
Correct |
40 ms |
5740 KB |
Output is correct |
21 |
Correct |
80 ms |
7476 KB |
Output is correct |
22 |
Correct |
99 ms |
8624 KB |
Output is correct |
23 |
Correct |
73 ms |
8608 KB |
Output is correct |
24 |
Correct |
169 ms |
17328 KB |
Output is correct |
25 |
Correct |
167 ms |
17408 KB |
Output is correct |