Submission #887257

#TimeUsernameProblemLanguageResultExecution timeMemory
887257vjudge1Swap (BOI16_swap)C++17
100 / 100
125 ms49220 KiB
#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';
}

Compilation message (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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...