답안 #752784

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
752784 2023-06-03T20:53:45 Z jampm 정렬하기 (IOI15_sorting) C++17
0 / 100
2 ms 852 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
const int LOGN = 30;

int count_cycles(vector<int> & V, int N = 0) {
  N = V.size(); int ans = 0;
  vector<bool> vis(N, false);
  for (int i = 0; i < N; i++) {
    if (vis[i]) continue;
    ans++;
    for (int j = V[i]; i != j; j = V[j]) vis[j] = true;
  }
  return ans;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
  int ans = 0;
  for (int k = LOGN; k >= 0; k--) {
    if (ans + (1<<k) > M) continue;
    vector<int> V(N);
    for (int i = 0; i < N; i++) V[i] = S[i];
    for (int i = 0; i < ans + (1<<k); i++) {
      swap(V[X[i]], V[Y[i]]);
    }
    if (count_cycles(V) + ans + (1<<k) < N) ans += (1<<k);
  }
  ans++;
  vector<int> V(N);
  for (int i = 0; i < N; i++) V[i] = S[i];
  for (int i = 0; i < ans; i++) {
    swap(V[X[i]], V[Y[i]]);
  }
  vector<bool> vis(N, false);
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (vis[i]) continue;
    P[cnt] = i, Q[cnt] = vis[i]; cnt++;
    for (int j = V[i]; i != j; j = V[j]) {
      P[cnt] = j, Q[cnt] = vis[j]; cnt++;
    }
  }
  return ans;
}

Compilation message

sorting.cpp: In function 'int count_cycles(std::vector<int>&, int)':
sorting.cpp:7:13: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
    7 |   N = V.size(); int ans = 0;
      |       ~~~~~~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Incorrect 1 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Incorrect 1 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 300 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Incorrect 1 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 852 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 852 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -