제출 #395024

#제출 시각아이디문제언어결과실행 시간메모리
395024WLZ정렬하기 (IOI15_sorting)C++14
100 / 100
190 ms21280 KiB
#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;
}


컴파일 시 표준 에러 (stderr) 메시지

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[]) {
      |                                   ~~~~^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...