답안 #259145

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
259145 2020-08-07T08:51:21 Z IOrtroiii 정렬하기 (IOI15_sorting) C++14
100 / 100
206 ms 18884 KB
#include "sorting.h"

#include <bits/stdc++.h>

using namespace std;

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
   auto solve = [&](int mid) -> bool {
      vector<int> perm(N);
      for (int i = 0; i < N; ++i) {
         perm[i] = S[i];
      }
      for (int i = 0; i < mid; ++i) swap(perm[X[i]], perm[Y[i]]);
      vector<int> invp(N);
      for (int i = 0; i < N; ++i) invp[perm[i]] = i;
      int ans = 0;
      for (int i = 0; i < N; ++i) {
         if (perm[i] == i) continue;
         int j = invp[i];
         P[ans] = i;
         Q[ans++] = j;
         swap(perm[i], perm[j]);
         invp[perm[i]] = i;
         invp[perm[j]] = j;
      }
      for (int i = ans; i < mid; ++i) P[i] = Q[i] = 0;
      return ans <= mid;
   };
   int low = 0, high = N;
   while (low < high) {
      int mid = (low + high) >> 1;
      if (solve(mid)) high = mid;
      else low = mid + 1;
   }
   solve(low);
   vector<int> perm(N);
   iota(perm.begin(), perm.end(), 0);
   vector<int> invp = perm;
   for (int i = low - 1; i >= 0; --i) {
      P[i] = invp[P[i]];
      Q[i] = invp[Q[i]];
      swap(perm[X[i]], perm[Y[i]]);
      invp[perm[X[i]]] = X[i];
      invp[perm[Y[i]]] = Y[i];
   }
   return low;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:7:39: warning: unused parameter 'M' [-Wunused-parameter]
 int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
                                       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 0 ms 256 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 169 ms 16756 KB Output is correct
16 Correct 174 ms 17148 KB Output is correct
17 Correct 205 ms 18148 KB Output is correct
18 Correct 78 ms 15712 KB Output is correct
19 Correct 149 ms 16804 KB Output is correct
20 Correct 169 ms 17464 KB Output is correct
21 Correct 163 ms 17440 KB Output is correct
22 Correct 164 ms 13344 KB Output is correct
23 Correct 184 ms 18728 KB Output is correct
24 Correct 201 ms 18408 KB Output is correct
25 Correct 199 ms 18164 KB Output is correct
26 Correct 154 ms 17452 KB Output is correct
27 Correct 141 ms 16824 KB Output is correct
28 Correct 195 ms 18348 KB Output is correct
29 Correct 190 ms 17824 KB Output is correct
30 Correct 121 ms 17100 KB Output is correct
31 Correct 185 ms 18268 KB Output is correct
32 Correct 185 ms 18192 KB Output is correct
33 Correct 200 ms 18436 KB Output is correct
34 Correct 186 ms 18296 KB Output is correct
35 Correct 149 ms 16476 KB Output is correct
36 Correct 76 ms 16816 KB Output is correct
37 Correct 206 ms 18884 KB Output is correct
38 Correct 184 ms 18208 KB Output is correct
39 Correct 197 ms 18212 KB Output is correct