답안 #426588

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
426588 2021-06-14T07:18:50 Z Peti 정렬하기 (IOI15_sorting) C++14
100 / 100
212 ms 20668 KB
#include <bits/stdc++.h>
#include "sorting.h"

using namespace std;

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

    int l = -1, r = M+1;
    while(l+1 < r){
        int m = (l+r)/2;
        vector<int> sor(N);
        for(int i = 0; i < N; i++)
            sor[i] = S[i];
        for(int i = 0; i < m; i++)
            swap(sor[X[i]], sor[Y[i]]);

        vector<bool> volt(N, false);
        int kor = 0;
        for(int i = 0; i < N; i++){
            if(volt[i]) continue;
            kor++;
            int x = sor[i];
            volt[i] = true;
            while(x != i){
                volt[x] = true;
                x = sor[x];
            }
        }
        if(N-kor <= m)
            r = m;
        else
            l = m;
    }

    vector<int> S2(N);
    for(int i = 0; i < N; i++)
        S2[i] = S[i];
    for(int i = 0; i < r; i++)
        swap(S[X[i]], S[Y[i]]);

    vector<bool> volt(N, false);
    vector<pair<int, int>> csere;
    for(int i = 0; i < N; i++){
        if(volt[i]) continue;
        int x = S[i];
        vector<int> v;
        while(x != i){
            v.push_back(x);
            volt[x] = true;
            x = S[x];
        }
        volt[i] = true;
        v.push_back(i);
        for(int j = 0; j < (int)v.size()-1; j++)
            csere.push_back(make_pair(v[j], v[j+1]));
    }

    int x = 0;
    for(int i = 0; i < r; i++){
        swap(inv[S2[X[i]]], inv[S2[Y[i]]]);
        swap(S2[X[i]], S2[Y[i]]);
        if(x < csere.size()){
            P[i] = inv[csere[x].first];
            Q[i] = inv[csere[x].second];
            swap(S2[inv[csere[x].first]], S2[inv[csere[x].second]]);
            swap(inv[csere[x].first], inv[csere[x].second]);
            x++;
        } else
            P[i] = Q[i] = 0;
    }

	return r;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:65:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |         if(x < csere.size()){
      |            ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 1 ms 284 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 292 KB Output is correct
14 Correct 1 ms 284 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 456 KB Output is correct
24 Correct 2 ms 428 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2 ms 452 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 432 KB Output is correct
10 Correct 2 ms 436 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 432 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2 ms 452 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 432 KB Output is correct
10 Correct 2 ms 436 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 432 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 153 ms 17264 KB Output is correct
16 Correct 165 ms 18372 KB Output is correct
17 Correct 172 ms 19100 KB Output is correct
18 Correct 79 ms 14828 KB Output is correct
19 Correct 142 ms 18156 KB Output is correct
20 Correct 212 ms 18780 KB Output is correct
21 Correct 152 ms 18784 KB Output is correct
22 Correct 146 ms 14004 KB Output is correct
23 Correct 165 ms 20668 KB Output is correct
24 Correct 176 ms 19156 KB Output is correct
25 Correct 180 ms 18916 KB Output is correct
26 Correct 164 ms 18728 KB Output is correct
27 Correct 148 ms 18140 KB Output is correct
28 Correct 176 ms 19588 KB Output is correct
29 Correct 170 ms 19148 KB Output is correct
30 Correct 128 ms 17236 KB Output is correct
31 Correct 167 ms 19532 KB Output is correct
32 Correct 172 ms 19276 KB Output is correct
33 Correct 171 ms 19944 KB Output is correct
34 Correct 171 ms 19648 KB Output is correct
35 Correct 148 ms 17932 KB Output is correct
36 Correct 64 ms 15912 KB Output is correct
37 Correct 188 ms 20396 KB Output is correct
38 Correct 184 ms 19504 KB Output is correct
39 Correct 168 ms 19824 KB Output is correct