Submission #368402

# Submission time Handle Problem Language Result Execution time Memory
368402 2021-02-20T03:04:35 Z KoD Sorting (IOI15_sorting) C++17
100 / 100
725 ms 15724 KB
#include <bits/stdc++.h>
#include "sorting.h"

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

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;
}
# 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 512 KB Output is correct
6 Correct 2 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 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 512 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 2 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 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 3 ms 364 KB Output is correct
4 Correct 2 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 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 512 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 2 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 512 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 3 ms 364 KB Output is correct
16 Correct 2 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 492 KB Output is correct
22 Correct 2 ms 492 KB Output is correct
23 Correct 4 ms 492 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
25 Correct 2 ms 492 KB Output is correct
26 Correct 2 ms 492 KB Output is correct
27 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 492 KB Output is correct
2 Correct 3 ms 492 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 6 ms 492 KB Output is correct
6 Correct 6 ms 492 KB Output is correct
7 Correct 7 ms 492 KB Output is correct
8 Correct 5 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 3 ms 492 KB Output is correct
13 Correct 3 ms 492 KB Output is correct
14 Correct 3 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 492 KB Output is correct
2 Correct 3 ms 492 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 6 ms 492 KB Output is correct
6 Correct 6 ms 492 KB Output is correct
7 Correct 7 ms 492 KB Output is correct
8 Correct 5 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 3 ms 492 KB Output is correct
13 Correct 3 ms 492 KB Output is correct
14 Correct 3 ms 364 KB Output is correct
15 Correct 406 ms 11480 KB Output is correct
16 Correct 450 ms 13760 KB Output is correct
17 Correct 381 ms 14616 KB Output is correct
18 Correct 350 ms 10960 KB Output is correct
19 Correct 556 ms 11644 KB Output is correct
20 Correct 442 ms 11932 KB Output is correct
21 Correct 432 ms 11936 KB Output is correct
22 Correct 430 ms 12080 KB Output is correct
23 Correct 354 ms 15724 KB Output is correct
24 Correct 519 ms 15324 KB Output is correct
25 Correct 485 ms 15524 KB Output is correct
26 Correct 441 ms 11776 KB Output is correct
27 Correct 416 ms 11332 KB Output is correct
28 Correct 482 ms 12476 KB Output is correct
29 Correct 504 ms 11916 KB Output is correct
30 Correct 642 ms 11220 KB Output is correct
31 Correct 725 ms 12144 KB Output is correct
32 Correct 509 ms 15024 KB Output is correct
33 Correct 443 ms 12688 KB Output is correct
34 Correct 500 ms 12304 KB Output is correct
35 Correct 407 ms 11444 KB Output is correct
36 Correct 221 ms 10744 KB Output is correct
37 Correct 481 ms 15704 KB Output is correct
38 Correct 435 ms 12680 KB Output is correct
39 Correct 592 ms 12784 KB Output is correct