이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, a[N];
unordered_map<int, int> mp[N];
int ind(int v, int x) {
if (mp[v].find(x) != mp[v].end()) return mp[v][x];
if (v * 2 > n) return mp[v][x] = v;
if (x < a[v * 2] && x < a[v * 2 + 1]) return mp[v][x] = v;
if (a[v * 2] < x && a[v * 2] < a[v * 2 + 1]) return mp[v][x] = ind(v * 2, x);
else {
int mn = min(x, a[v * 2]), mx = max(x, a[v * 2]);
if (ind(v * 2, mn) < ind(v * 2 + 1, mn))
if (x == mn) return mp[v][x] = ind(v * 2, x);
else return mp[v][x] = ind(v * 2 + 1, x);
else
if (x == mn) return mp[v][x] = ind(v * 2 + 1, x);
else return mp[v][x] = ind(v * 2, x);
}
}
int main() {
ios:: sync_with_stdio(0), cin.tie(0);
cin >> n;
memset(a, 63, sizeof a);
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
if (i * 2 > n) continue;
if (a[i] < a[i * 2] && a[i] < a[i * 2 + 1]) continue;
if (a[i * 2] < a[i] && a[i * 2] < a[i * 2 + 1]) swap(a[i * 2], a[i]);
else {
int mn = min(a[i], a[i * 2]), mx = max(a[i], a[i * 2]);
swap(a[i * 2 + 1], a[i]);
if (ind(i * 2, mn) < ind(i * 2 + 1, mn)) a[i * 2] = mn, a[i * 2 + 1] = mx;
else a[i * 2] = mx, a[i * 2 + 1] = mn;
}
}
for (int i = 1; i <= n; i++) cout << a[i] << ' ';
cout << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
swap.cpp: In function 'int ind(int, int)':
swap.cpp:15:36: warning: unused variable 'mx' [-Wunused-variable]
15 | int mn = min(x, a[v * 2]), mx = max(x, a[v * 2]);
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |