Submission #726596

# Submission time Handle Problem Language Result Execution time Memory
726596 2023-04-19T06:14:57 Z becaido Sorting (IOI15_sorting) C++17
100 / 100
155 ms 10700 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "sorting.h"
#endif

const int N = 2e5 + 5;

int n;
int a[N];
bool vs[N];

bool ok(int r, int S[], int X[], int Y[]) {
    for (int i = 0; i < n; i++) a[i] = S[i], vs[i] = 0;
    for (int i = 0; i < r; i++) swap(a[X[i]], a[Y[i]]);
    int cnt = n;
    for (int i = 0; i < n; i++) if (!vs[i]) {
        cnt--;
        for (int cur = i; !vs[cur]; cur = a[cur]) vs[cur] = 1;
    }
    return cnt <= r;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    n = N;
    int l = 0, r = M;
    while (l < r) {
        int mid = (l + r) / 2;
        if (ok(mid, S, X, Y)) r = mid;
        else l = mid + 1;
    }
    for (int i = 0; i < n; i++) a[i] = S[i], vs[i] = 0;
    for (int i = 0; i < r; i++) swap(a[X[i]], a[Y[i]]);
    int sz = 0;
    for (int i = 0; i < n; i++) if (!vs[i]) {
        int last = -1, cur = i;
        while (!vs[cur]) {
            vs[cur] = 1;
            if (last != -1) P[sz] = last, Q[sz] = cur, sz++;
            last = cur;
            cur = a[cur];
        }
    }
    while (sz < r) P[sz] = Q[sz] = 0, sz++;
    for (int i = 0; i < n; i++) a[S[i]] = i;
    for (int i = 0; i < r; i++) {
        swap(a[S[X[i]]], a[S[Y[i]]]);
        swap(S[X[i]], S[Y[i]]);
        P[i] = a[P[i]], Q[i] = a[Q[i]];
        swap(a[S[P[i]]], a[S[Q[i]]]);
        swap(S[P[i]], S[Q[i]]);
    }
    return r;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:27:23: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   27 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                   ~~~~^
sorting.cpp:10:11: note: shadowed declaration is here
   10 | const int N = 2e5 + 5;
      |           ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 119 ms 9616 KB Output is correct
16 Correct 130 ms 9932 KB Output is correct
17 Correct 141 ms 10372 KB Output is correct
18 Correct 48 ms 6224 KB Output is correct
19 Correct 119 ms 8356 KB Output is correct
20 Correct 119 ms 8780 KB Output is correct
21 Correct 121 ms 8816 KB Output is correct
22 Correct 112 ms 10520 KB Output is correct
23 Correct 135 ms 10700 KB Output is correct
24 Correct 143 ms 10576 KB Output is correct
25 Correct 143 ms 10488 KB Output is correct
26 Correct 123 ms 8780 KB Output is correct
27 Correct 105 ms 8316 KB Output is correct
28 Correct 144 ms 10352 KB Output is correct
29 Correct 155 ms 9964 KB Output is correct
30 Correct 90 ms 7628 KB Output is correct
31 Correct 153 ms 10136 KB Output is correct
32 Correct 136 ms 10192 KB Output is correct
33 Correct 150 ms 10564 KB Output is correct
34 Correct 143 ms 10472 KB Output is correct
35 Correct 126 ms 8380 KB Output is correct
36 Correct 41 ms 6716 KB Output is correct
37 Correct 155 ms 10308 KB Output is correct
38 Correct 153 ms 10320 KB Output is correct
39 Correct 150 ms 9748 KB Output is correct