답안 #796439

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
796439 2023-07-28T11:41:10 Z Josia 정렬하기 (IOI15_sorting) C++17
100 / 100
140 ms 27508 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]]);
    }

    assert(is_sorted(mySwapsValues.begin(), mySwapsValues.end()));

    assert(mySwapsValues.size() == l || mySwapsValues.size()+1 == l);

    if (mySwapsValues.size() < l) {
        P[l-1]=0;
        Q[l-1]=0;
    }

    return l;
}

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++) {
      |                     ~^~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from sorting.cpp:2:
sorting.cpp:104:33: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  104 |     assert(mySwapsValues.size() == l || mySwapsValues.size()+1 == l);
      |            ~~~~~~~~~~~~~~~~~~~~~^~~~
sorting.cpp:104:64: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  104 |     assert(mySwapsValues.size() == l || mySwapsValues.size()+1 == l);
      |                                         ~~~~~~~~~~~~~~~~~~~~~~~^~~~
sorting.cpp:106:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  106 |     if (mySwapsValues.size() < l) {
      |         ~~~~~~~~~~~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 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 0 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 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 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 0 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 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 684 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 1 ms 724 KB Output is correct
27 Correct 1 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 436 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 564 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 564 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 436 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 564 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 564 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 112 ms 25120 KB Output is correct
16 Correct 119 ms 25520 KB Output is correct
17 Correct 133 ms 26516 KB Output is correct
18 Correct 68 ms 23076 KB Output is correct
19 Correct 116 ms 24728 KB Output is correct
20 Correct 123 ms 25152 KB Output is correct
21 Correct 119 ms 25256 KB Output is correct
22 Correct 106 ms 22640 KB Output is correct
23 Correct 135 ms 27224 KB Output is correct
24 Correct 131 ms 26960 KB Output is correct
25 Correct 129 ms 26724 KB Output is correct
26 Correct 120 ms 25220 KB Output is correct
27 Correct 107 ms 24748 KB Output is correct
28 Correct 129 ms 26800 KB Output is correct
29 Correct 131 ms 26384 KB Output is correct
30 Correct 94 ms 24476 KB Output is correct
31 Correct 130 ms 26800 KB Output is correct
32 Correct 132 ms 26532 KB Output is correct
33 Correct 140 ms 26824 KB Output is correct
34 Correct 131 ms 26876 KB Output is correct
35 Correct 117 ms 24420 KB Output is correct
36 Correct 62 ms 24284 KB Output is correct
37 Correct 136 ms 27508 KB Output is correct
38 Correct 129 ms 26688 KB Output is correct
39 Correct 130 ms 26856 KB Output is correct