Submission #433061

# Submission time Handle Problem Language Result Execution time Memory
433061 2021-06-18T19:27:26 Z timmyfeng Sorting (IOI15_sorting) C++17
100 / 100
332 ms 15192 KB
#include <bits/stdc++.h>
using namespace std;

#include "sorting.h"

const int N = 200000;

int perm[N], inv[N], first[N], second[N];
bool visited[N];

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    int low = 0, high = M + 1;
    while (low < high) {
        int mid = (low + high) / 2;

        copy(S, S + N, perm);
        for (int i = 0; i < mid; ++i) {
            swap(perm[X[i]], perm[Y[i]]);
        }

        int count = 0;
        fill(visited, visited + N, false);
        fill(first, first + N, 0);
        fill(second, second + N, 0);
        for (int i = 0; i < N; ++i) {
            while (perm[i] != i) {
                first[count] = perm[i], second[count] = perm[perm[i]], ++count;
                swap(perm[i], perm[perm[i]]);
            }
        }

        if (count <= mid) {
            high = mid;

            copy(S, S + N, perm);
            for (int i = 0; i < N; ++i) {
                inv[perm[i]] = i;
            }

            for (int i = 0; i < mid; ++i) {
                swap(perm[X[i]], perm[Y[i]]);
                swap(inv[perm[X[i]]], inv[perm[Y[i]]]);
                P[i] = first[i], Q[i] = second[i];
                swap(inv[P[i]], inv[Q[i]]);
                P[i] = inv[P[i]], Q[i] = inv[Q[i]];
                swap(perm[P[i]], perm[Q[i]]);
            }
        } else {
            low = mid + 1;
        }
    }
    return low;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:11:23: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   11 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                   ~~~~^
sorting.cpp:6:11: note: shadowed declaration is here
    6 | const int N = 200000;
      |           ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 236 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 236 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 236 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 239 ms 12152 KB Output is correct
16 Correct 237 ms 12484 KB Output is correct
17 Correct 293 ms 14020 KB Output is correct
18 Correct 108 ms 11408 KB Output is correct
19 Correct 210 ms 13644 KB Output is correct
20 Correct 218 ms 14020 KB Output is correct
21 Correct 205 ms 14020 KB Output is correct
22 Correct 237 ms 12748 KB Output is correct
23 Correct 282 ms 14304 KB Output is correct
24 Correct 290 ms 14228 KB Output is correct
25 Correct 300 ms 14412 KB Output is correct
26 Correct 213 ms 13892 KB Output is correct
27 Correct 185 ms 13508 KB Output is correct
28 Correct 256 ms 14004 KB Output is correct
29 Correct 291 ms 14276 KB Output is correct
30 Correct 160 ms 13260 KB Output is correct
31 Correct 332 ms 14664 KB Output is correct
32 Correct 236 ms 13588 KB Output is correct
33 Correct 312 ms 14520 KB Output is correct
34 Correct 242 ms 14284 KB Output is correct
35 Correct 236 ms 13424 KB Output is correct
36 Correct 79 ms 12612 KB Output is correct
37 Correct 311 ms 15192 KB Output is correct
38 Correct 288 ms 14276 KB Output is correct
39 Correct 302 ms 14820 KB Output is correct