답안 #1080012

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1080012 2024-08-29T06:09:16 Z juicy 정렬하기 (IOI15_sorting) C++17
100 / 100
115 ms 24660 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) {
  vector<int> a(n), pos(n);
  vector<array<int, 2>> ops(m);
  auto count = [&](int k) {
    for (int i = 0; i < n; ++i) {
      a[i] = s[i];
    }
    for (int i = 0; i < k; ++i) {
      swap(a[x[i]], a[y[i]]);
    }
    for (int i = 0; i < n; ++i) {
      pos[a[i]] = i;
    }
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
      if (i == a[i]) {
        continue;
      }
      int j = pos[i];
      ops[cnt++] = {a[i], a[j]};
      swap(pos[a[i]], pos[a[j]]);
      swap(a[i], a[j]);
    }
    return cnt;
  };
  int l = 0, r = m, k = m;
  while (l <= r) {
    int md = (l + r) / 2;
    if (count(md) <= md) {
      k = md;
      r = md - 1;
    } else {
      l = md + 1;
    }
  }
  auto cnt = count(k);
  for (int i = cnt; i < k; ++i) {
    ops[i] = {0, 0};
  }
  for (int i = 0; i < n; ++i) {
    pos[s[i]] = i;
  }
  auto swp = [&](int i, int j) {
    swap(pos[s[i]], pos[s[j]]);
    swap(s[i], s[j]);
  };
  for (int i = 0; i < k; ++i) {
    swp(x[i], y[i]);
    auto [x, y] = ops[i];
    tie(p[i], q[i]) = tie(pos[x], pos[y]);
    swp(p[i], q[i]);
  }
  return k;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:55:11: warning: declaration of 'auto x' shadows a parameter [-Wshadow]
   55 |     auto [x, y] = ops[i];
      |           ^
sorting.cpp:7:46: note: shadowed declaration is here
    7 | int findSwapPairs(int n, int *s, int m, int *x, int *y, int *p, int *q) {
      |                                         ~~~~~^
sorting.cpp:55:14: warning: declaration of 'auto y' shadows a parameter [-Wshadow]
   55 |     auto [x, y] = ops[i];
      |              ^
sorting.cpp:7:54: note: shadowed declaration is here
    7 | int findSwapPairs(int n, int *s, int m, int *x, int *y, int *p, int *q) {
      |                                                 ~~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 440 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 440 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 440 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 708 KB Output is correct
27 Correct 1 ms 584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 700 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 508 KB Output is correct
7 Correct 1 ms 608 KB Output is correct
8 Correct 1 ms 612 KB Output is correct
9 Correct 1 ms 616 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 548 KB Output is correct
12 Correct 1 ms 612 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 700 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 508 KB Output is correct
7 Correct 1 ms 608 KB Output is correct
8 Correct 1 ms 612 KB Output is correct
9 Correct 1 ms 616 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 548 KB Output is correct
12 Correct 1 ms 612 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 612 KB Output is correct
15 Correct 94 ms 19832 KB Output is correct
16 Correct 92 ms 20240 KB Output is correct
17 Correct 101 ms 21380 KB Output is correct
18 Correct 51 ms 19280 KB Output is correct
19 Correct 85 ms 21200 KB Output is correct
20 Correct 88 ms 21860 KB Output is correct
21 Correct 89 ms 21660 KB Output is correct
22 Correct 75 ms 16724 KB Output is correct
23 Correct 103 ms 23632 KB Output is correct
24 Correct 115 ms 24660 KB Output is correct
25 Correct 100 ms 21528 KB Output is correct
26 Correct 90 ms 21584 KB Output is correct
27 Correct 80 ms 21332 KB Output is correct
28 Correct 96 ms 23356 KB Output is correct
29 Correct 94 ms 21552 KB Output is correct
30 Correct 68 ms 20896 KB Output is correct
31 Correct 96 ms 21840 KB Output is correct
32 Correct 108 ms 21328 KB Output is correct
33 Correct 102 ms 21784 KB Output is correct
34 Correct 98 ms 21920 KB Output is correct
35 Correct 85 ms 20684 KB Output is correct
36 Correct 47 ms 20584 KB Output is correct
37 Correct 107 ms 23888 KB Output is correct
38 Correct 101 ms 21584 KB Output is correct
39 Correct 104 ms 21588 KB Output is correct