답안 #395024

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
395024 2021-04-27T15:55:55 Z WLZ 정렬하기 (IOI15_sorting) C++14
100 / 100
190 ms 21280 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 check = [&](int k) {
    vector<int> s(S, S + N), pos(N);
    for (int i = 0; i < k; i++) swap(s[X[i]], s[Y[i]]);
    for (int i = 0; i < N; i++) pos[s[i]] = i;
    int cnt = 0;
    for (int i = 0; i < N; i++) {
      if (pos[i] != i) {
        cnt++;
        pos[s[i]] = pos[i];
        swap(s[i], s[pos[i]]);
        pos[i] = i;
      }
    }
    return cnt <= k;
  };

  int lo = 0, hi = N - 1;
  while (lo < hi) {
    int mid = lo + (hi - lo) / 2;
    if (check(mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  vector<int> s(S, S + N), pos(N);
  for (int i = 0; i < lo; i++) swap(s[X[i]], s[Y[i]]);
  for (int i = 0; i < N; i++) pos[s[i]] = i;
  vector< pair<int, int> > swaps;
  for (int i = 0; i < N; i++) {
    if (pos[i] != i) {
      pos[s[i]] = pos[i];
      swaps.push_back({i, s[i]});
      swap(s[i], s[pos[i]]);
      pos[i] = i;
    }
  }
  s = vector<int>(S, S + N);
  for (int i = 0; i < N; i++) pos[s[i]] = i;
  for (int i = 0; i < lo; i++) {
    swap(pos[s[X[i]]], pos[s[Y[i]]]);
    swap(s[X[i]], s[Y[i]]);
    if (i < (int) swaps.size()) P[i] = pos[swaps[i].first], Q[i] = pos[swaps[i].second]; 
    else P[i] = Q[i] = 0;
    swap(pos[s[P[i]]], pos[s[Q[i]]]);
    swap(s[P[i]], s[Q[i]]);
  }
  return lo;
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:5:39: warning: unused parameter 'M' [-Wunused-parameter]
    5 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 216 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 216 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 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 1 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 1 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 1 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 1 ms 436 KB Output is correct
24 Correct 2 ms 444 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 2 ms 408 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 2 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 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 3 ms 460 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 2 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 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 3 ms 460 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 152 ms 18856 KB Output is correct
16 Correct 168 ms 19492 KB Output is correct
17 Correct 163 ms 20336 KB Output is correct
18 Correct 74 ms 15548 KB Output is correct
19 Correct 134 ms 18568 KB Output is correct
20 Correct 147 ms 19180 KB Output is correct
21 Correct 151 ms 19288 KB Output is correct
22 Correct 138 ms 15624 KB Output is correct
23 Correct 169 ms 21092 KB Output is correct
24 Correct 185 ms 20696 KB Output is correct
25 Correct 183 ms 20540 KB Output is correct
26 Correct 143 ms 19328 KB Output is correct
27 Correct 138 ms 18576 KB Output is correct
28 Correct 167 ms 20568 KB Output is correct
29 Correct 170 ms 20036 KB Output is correct
30 Correct 109 ms 18004 KB Output is correct
31 Correct 166 ms 20576 KB Output is correct
32 Correct 158 ms 20280 KB Output is correct
33 Correct 171 ms 20708 KB Output is correct
34 Correct 180 ms 20672 KB Output is correct
35 Correct 137 ms 18452 KB Output is correct
36 Correct 71 ms 16764 KB Output is correct
37 Correct 190 ms 21280 KB Output is correct
38 Correct 156 ms 20480 KB Output is correct
39 Correct 179 ms 20692 KB Output is correct