Submission #368401

# Submission time Handle Problem Language Result Execution time Memory
368401 2021-02-20T03:02:53 Z KoD Sorting (IOI15_sorting) C++17
100 / 100
531 ms 24544 KB
#ifndef LOCAL
#include "sorting.h"
#endif

#include <vector>
#include <iostream>
#include <numeric>
#include <algorithm>
#include <queue>

template <class T>
using Vec = std::vector<T>;

struct DSU {
    Vec<int> par;
    DSU(const int n): par(n, -1) { }
    int find(const int u) {
        return par[u] < 0 ? u : par[u] = find(par[u]);
    }
    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return false;
        }
        if (par[x] > par[y]) {
            std::swap(x, y);
        }
        par[x] += par[y];
        par[y] = x;
        return true;
    }
};

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    int ok = M, ng = -1;
    while (ok - ng > 1) {
        int md = (ok + ng) / 2;
        Vec<int> perm(N);
        Vec<int> inv(N);
        std::iota(perm.begin(), perm.end(), 0);
        std::iota(inv.begin(), inv.end(), 0);
        const auto swap = [&](const int i, const int j) {
            std::swap(perm[i], perm[j]);
            inv[perm[i]] = i;
            inv[perm[j]] = j;
        };
        for (int i = 0; i < md; ++i) {
            swap(X[i], Y[i]);
        }
        std::queue<std::pair<int, int>> que;
        {
            Vec<int> to(N);
            for (int i = 0; i < N; ++i) {
                to[S[i]] = inv[i];
            }
            Vec<char> used(N);
            for (int i = 0; i < N; ++i) {
                if (!used[i]) {
                    Vec<int> path;
                    {
                        int u = i;
                        while (!used[u]) {
                            path.push_back(u);
                            used[u] = true;
                            u = to[u];
                        }
                    }
                    for (int j = 0; j + 1 < (int) path.size(); ++j) {
                        que.emplace(path[0], path[j + 1]);
                    }
                }
            }
        }
        if ((int) que.size() > md) {
            ng = md;
            continue;
        }
        ok = md;
        for (int i = 0; i < N; ++i) {
            perm[i] = S[i];
            inv[S[i]] = i;
        }
        for (int i = 0; i < md; ++i) {
            swap(X[i], Y[i]);
            if (que.empty()) {
                P[i] = Q[i] = 0;
            }   
            else {
                const auto [x, y] = que.front();
                que.pop();
                const auto s = inv[x];
                const auto t = inv[y];
                P[i] = s;
                Q[i] = t;
                swap(s, t);
            }
        }
    }
    return ok;
}

#ifdef LOCAL
int main() {
    int N;
    std::cin >> N;
    int S[100] = {};
    for (int i = 0; i < N; ++i) {
        std::cin >> S[i];
    }
    int M;
    std::cin >> M;
    int X[100] = {};
    int Y[100] = {};
    for (int i = 0; i < M; ++i) {
        std::cin >> X[i] >> Y[i];
    }
    int P[100] = {};
    int Q[100] = {};
    int R = findSwapPairs(N, S, M, X, Y, P, Q);
    for (int i = 0; i < R; ++i) {
        std::swap(S[X[i]], S[Y[i]]);
        std::swap(S[P[i]], S[Q[i]]);
    }
    std::cout << R << '\n';
    for (int i = 0; i < N; ++i) {
        std::cout << S[i] << " \n"[i + 1 == N];
    }
    return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 2 ms 620 KB Output is correct
22 Correct 2 ms 620 KB Output is correct
23 Correct 2 ms 620 KB Output is correct
24 Correct 2 ms 620 KB Output is correct
25 Correct 3 ms 620 KB Output is correct
26 Correct 2 ms 620 KB Output is correct
27 Correct 2 ms 620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 3 ms 620 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 3 ms 492 KB Output is correct
6 Correct 6 ms 492 KB Output is correct
7 Correct 4 ms 492 KB Output is correct
8 Correct 3 ms 492 KB Output is correct
9 Correct 3 ms 492 KB Output is correct
10 Correct 3 ms 492 KB Output is correct
11 Correct 3 ms 492 KB Output is correct
12 Correct 4 ms 492 KB Output is correct
13 Correct 3 ms 492 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 3 ms 620 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 3 ms 492 KB Output is correct
6 Correct 6 ms 492 KB Output is correct
7 Correct 4 ms 492 KB Output is correct
8 Correct 3 ms 492 KB Output is correct
9 Correct 3 ms 492 KB Output is correct
10 Correct 3 ms 492 KB Output is correct
11 Correct 3 ms 492 KB Output is correct
12 Correct 4 ms 492 KB Output is correct
13 Correct 3 ms 492 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
15 Correct 342 ms 19108 KB Output is correct
16 Correct 420 ms 21972 KB Output is correct
17 Correct 409 ms 22920 KB Output is correct
18 Correct 331 ms 19124 KB Output is correct
19 Correct 411 ms 20220 KB Output is correct
20 Correct 479 ms 20764 KB Output is correct
21 Correct 410 ms 20784 KB Output is correct
22 Correct 456 ms 15684 KB Output is correct
23 Correct 463 ms 24428 KB Output is correct
24 Correct 431 ms 23904 KB Output is correct
25 Correct 428 ms 23972 KB Output is correct
26 Correct 456 ms 20552 KB Output is correct
27 Correct 403 ms 19940 KB Output is correct
28 Correct 460 ms 21088 KB Output is correct
29 Correct 531 ms 20264 KB Output is correct
30 Correct 350 ms 19900 KB Output is correct
31 Correct 505 ms 20760 KB Output is correct
32 Correct 437 ms 23344 KB Output is correct
33 Correct 467 ms 21304 KB Output is correct
34 Correct 437 ms 20776 KB Output is correct
35 Correct 410 ms 19964 KB Output is correct
36 Correct 225 ms 19388 KB Output is correct
37 Correct 415 ms 24544 KB Output is correct
38 Correct 480 ms 21404 KB Output is correct
39 Correct 407 ms 21500 KB Output is correct