답안 #1015326

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1015326 2024-07-06T08:44:50 Z 우민규(#10889) 정렬하기 (IOI15_sorting) C++14
100 / 100
184 ms 26720 KB
#include "sorting.h"

#include <bits/stdc++.h>
using namespace std;

// can swap -> (valid swapping)
// can't -> {}
vector<pair<int, int>> can_sort(vector<int> perm, int m, int* x, int* y) {
  for (int i = 0; i < m; ++i) swap(perm[x[i]], perm[y[i]]);
  // sort perm
  vector<pair<int, int>> sorting;
  for (int i = 0; i < perm.size(); ++i) {
    while (perm[i] != i) {
      sorting.push_back({i, perm[i]});
      swap(perm[i], perm[perm[i]]);
    }
  }
  if (sorting.size() > m) {
    return {};
  }
  while (sorting.size() < m) sorting.push_back({0, 0});
  vector<int> swap_perm(perm.size()), swap_perm_inv(perm.size());
  iota(swap_perm.begin(), swap_perm.end(), 0);
  iota(swap_perm_inv.begin(), swap_perm_inv.end(), 0);
  for (int i = m - 1; i >= 0; --i) {
    swap(swap_perm[x[i]], swap_perm[y[i]]);
    swap(swap_perm_inv[swap_perm[x[i]]], swap_perm_inv[swap_perm[y[i]]]);
  }
  for (int i = 0; i < m; ++i) {
    swap(swap_perm[x[i]], swap_perm[y[i]]);
    swap(swap_perm_inv[swap_perm[x[i]]], swap_perm_inv[swap_perm[y[i]]]);
    sorting[i].first = swap_perm_inv[sorting[i].first];
    sorting[i].second = swap_perm_inv[sorting[i].second];
  }
  return sorting;
}

int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
  bool is_zero = true;
  for (int i = 0; i < n; ++i)
    if (s[i] != i) is_zero = false;
  if (is_zero) return 0;
  // rl(0) -> not possible
  // rh(m) -> possible
  int rl = 0, rh = m;
  vector<pair<int, int>> pos_sorting = can_sort(vector<int>(s, s + n), m, x, y);
  while (rl + 1 < rh) {
    int rm = (rl + rh) / 2;
    vector<pair<int, int>> sorting = can_sort(vector<int>(s, s + n), rm, x, y);
    // not possible
    if (sorting.size() == 0) {
      rl = rm;
    } else {
      rh = rm;
      pos_sorting = sorting;
    }
  }
  for (int i = 0; i < rh; ++i) {
    p[i] = pos_sorting[i].first;
    q[i] = pos_sorting[i].second;
  }
  return rh;
}

#ifndef EVAL
#include "grader.cpp"
#endif

Compilation message

sorting.cpp: In function 'std::vector<std::pair<int, int> > can_sort(std::vector<int>, int, int*, int*)':
sorting.cpp:12:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |   for (int i = 0; i < perm.size(); ++i) {
      |                   ~~^~~~~~~~~~~~~
sorting.cpp:18:22: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   18 |   if (sorting.size() > m) {
      |       ~~~~~~~~~~~~~~~^~~
sorting.cpp:21:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   21 |   while (sorting.size() < m) sorting.push_back({0, 0});
      |          ~~~~~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 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 348 KB Output is correct
5 Correct 0 ms 348 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 348 KB Output is correct
5 Correct 0 ms 348 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 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 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 344 KB Output is correct
5 Correct 1 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 348 KB Output is correct
5 Correct 0 ms 348 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 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 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 0 ms 348 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 824 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 856 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 856 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 140 ms 23748 KB Output is correct
16 Correct 158 ms 24312 KB Output is correct
17 Correct 174 ms 25416 KB Output is correct
18 Correct 36 ms 13436 KB Output is correct
19 Correct 129 ms 26048 KB Output is correct
20 Correct 134 ms 26644 KB Output is correct
21 Correct 126 ms 26720 KB Output is correct
22 Correct 142 ms 20660 KB Output is correct
23 Correct 149 ms 26184 KB Output is correct
24 Correct 176 ms 26048 KB Output is correct
25 Correct 172 ms 25744 KB Output is correct
26 Correct 120 ms 26532 KB Output is correct
27 Correct 105 ms 26040 KB Output is correct
28 Correct 154 ms 26048 KB Output is correct
29 Correct 170 ms 25892 KB Output is correct
30 Correct 97 ms 26260 KB Output is correct
31 Correct 174 ms 26292 KB Output is correct
32 Correct 147 ms 25552 KB Output is correct
33 Correct 184 ms 25796 KB Output is correct
34 Correct 161 ms 25792 KB Output is correct
35 Correct 121 ms 25608 KB Output is correct
36 Correct 63 ms 26320 KB Output is correct
37 Correct 180 ms 26476 KB Output is correct
38 Correct 167 ms 25548 KB Output is correct
39 Correct 180 ms 25672 KB Output is correct