Submission #102713

# Submission time Handle Problem Language Result Execution time Memory
102713 2019-03-27T08:08:27 Z wxh010910 Sorting (IOI15_sorting) C++17
100 / 100
224 ms 18760 KB
#include <bits/stdc++.h>
#include "sorting.h"

using namespace std;

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
  vector<bool> visit(N);
  vector<int> after(N);
  auto check = [&](int mid) {
    for (int i = 0; i < N; ++i) {
      after[i] = S[i];
      visit[i] = false;
    }
    for (int i = 0; i < mid; ++i) {
      swap(after[X[i]], after[Y[i]]);
    }
    int ans = 0;
    for (int i = 0; i < N; ++i) {
      if (!visit[i]) {
        for (int j = i; ; j = after[j]) {
          visit[j] = true;
          if (!visit[after[j]]) {
            P[ans] = i;
            Q[ans] = after[j];
            ++ans;
          } else {
            break;
          }
        }
      }
    }
    for (int i = ans; i < mid; ++i) {
      P[i] = Q[i] = 0;
    }
    return ans <= mid;
  };
  int l = 0, r = N;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  check(r);
  vector<int> pos(N);
  vector<int> to(N);
  for (int i = 0; i < N; ++i) {
    pos[i] = to[i] = i;
  }
  for (int i = r - 1; ~i; --i) {
    P[i] = to[P[i]];
    Q[i] = to[Q[i]];
    swap(pos[X[i]], pos[Y[i]]);
    to[pos[X[i]]] = X[i];
    to[pos[Y[i]]] = Y[i];
  }
  return r;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:6:39: warning: unused parameter 'M' [-Wunused-parameter]
 int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
                                       ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 404 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 404 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 404 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 256 KB Output is correct
20 Correct 2 ms 256 KB Output is correct
21 Correct 4 ms 512 KB Output is correct
22 Correct 4 ms 512 KB Output is correct
23 Correct 4 ms 640 KB Output is correct
24 Correct 4 ms 512 KB Output is correct
25 Correct 4 ms 640 KB Output is correct
26 Correct 4 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 6 ms 640 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 4 ms 512 KB Output is correct
14 Correct 4 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 512 KB Output is correct
2 Correct 4 ms 512 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 6 ms 640 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 4 ms 512 KB Output is correct
14 Correct 4 ms 640 KB Output is correct
15 Correct 142 ms 16280 KB Output is correct
16 Correct 159 ms 16672 KB Output is correct
17 Correct 193 ms 17784 KB Output is correct
18 Correct 67 ms 16376 KB Output is correct
19 Correct 166 ms 17400 KB Output is correct
20 Correct 164 ms 18268 KB Output is correct
21 Correct 168 ms 18168 KB Output is correct
22 Correct 182 ms 12892 KB Output is correct
23 Correct 161 ms 18424 KB Output is correct
24 Correct 206 ms 18040 KB Output is correct
25 Correct 202 ms 17912 KB Output is correct
26 Correct 150 ms 18168 KB Output is correct
27 Correct 130 ms 17656 KB Output is correct
28 Correct 189 ms 18168 KB Output is correct
29 Correct 188 ms 17912 KB Output is correct
30 Correct 102 ms 17500 KB Output is correct
31 Correct 183 ms 18468 KB Output is correct
32 Correct 153 ms 17656 KB Output is correct
33 Correct 224 ms 18012 KB Output is correct
34 Correct 169 ms 18040 KB Output is correct
35 Correct 143 ms 17144 KB Output is correct
36 Correct 60 ms 17556 KB Output is correct
37 Correct 221 ms 18760 KB Output is correct
38 Correct 213 ms 17912 KB Output is correct
39 Correct 192 ms 18200 KB Output is correct