답안 #796433

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
796433 2023-07-28T11:38:02 Z Josia 정렬하기 (IOI15_sorting) C++17
20 / 100
1 ms 468 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;


int countCycles(vector<int> p) {
    vector<bool> vis(p.size());
    int count=0;

    for (int i = 0; i<(int)p.size(); i++) {
        if (!vis[i]) count++;
        int pos = i;
        while(!vis[pos]) {
            vis[pos] = 1;
            pos = p[pos];
        }
    }
    return count;
}


vector<int> doSwaps(vector<int> input, vector<pair<int, int>> swaps, int number) {
    for (int i = 0; i<number; i++) {
        swap(input[swaps[i].first], input[swaps[i].second]);
    }
    return input;
}


int findSwapPairs(int N, int _S[], int M, int X[], int Y[], int P[], int Q[]) {
	vector<int> S(N);
	for (int i = 0; i<N; i++) S[i] = _S[i];

    vector<pair<int, int>> swapsOther;
    for (int i = 0; i<M; i++) swapsOther.push_back({X[i], Y[i]});


    int l = 0, r = M;

    while (l < r) {
        int m = (l+r)/2;
        int cycles = countCycles(doSwaps(S, swapsOther, m));
        // cerr << m << " " << cycles << "\n";

        if (N-cycles <= m) {
            r = m;
        } else {
            l = m+1;
        }
    }

    vector<int> hisPerm = doSwaps(S, swapsOther, l);

    // cerr << l << "\n";
    // for (int i: hisPerm) cerr << i << " ";
    // cerr << "\n";

    vector<pair<int, int>> mySwapsValues;

    vector<int> hisPermInv(N);

    for (int i = 0; i<N; i++) {
        hisPermInv[hisPerm[i]] = i;
    }

    for (int i = 0; i<N; i++) {
        if (hisPerm[i] == i) continue;

        mySwapsValues.push_back({i, hisPerm[i]});

        int miau = hisPerm[i];

        swap(hisPerm[hisPermInv[i]], hisPerm[i]);
        swap(hisPermInv[i], hisPermInv[miau]);
    }
    // for (int i: hisPerm) cerr << i << " ";
    // cerr << "\n";
    assert(is_sorted(hisPerm.begin(), hisPerm.end()));

    // for (auto i: mySwapsValues) {cerr << i.first << " " << i.second << "\n";}


    vector<int> SInv(N);

    for (int i = 0; i<N; i++) SInv[S[i]] = i;


    for (int i = 0; i<mySwapsValues.size(); i++) {
        swap(SInv[S[swapsOther[i].first]], SInv[S[swapsOther[i].second]]);
        swap(S[swapsOther[i].first], S[swapsOther[i].second]);

        P[i] = SInv[mySwapsValues[i].first];
        Q[i] = SInv[mySwapsValues[i].second];

        assert(S[P[i]] == mySwapsValues[i].first);
        assert(S[Q[i]] == mySwapsValues[i].second);

        swap(SInv[S[P[i]]], SInv[S[Q[i]]]);
        swap(S[P[i]], S[Q[i]]);
    }

    return mySwapsValues.size();
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:88:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |     for (int i = 0; i<mySwapsValues.size(); i++) {
      |                     ~^~~~~~~~~~~~~~~~~~~~~
sorting.cpp:102:30: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  102 |     return mySwapsValues.size();
      |            ~~~~~~~~~~~~~~~~~~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Incorrect 1 ms 212 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Incorrect 1 ms 212 KB Output isn't correct
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Incorrect 1 ms 468 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Incorrect 1 ms 468 KB Output isn't correct
4 Halted 0 ms 0 KB -