답안 #590907

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
590907 2022-07-06T13:59:57 Z AlperenT 정렬하기 (IOI15_sorting) C++17
100 / 100
161 ms 11636 KB
#include <bits/stdc++.h>
#include "sorting.h"

using namespace std;

int swapcnt(vector<int> v){
    int cnt = 0, n = v.size();

    vector<bool> vis(n, false);

    for(int i = 0; i < n; i++){
        if(!vis[i]){
            int x = i;

            while(!vis[x]){
                cnt++;

                vis[x] = true;
                x = v[x];
            }

            cnt--;
        }
    }

    return cnt;
}

int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]){
    vector<int> v, v2;

    v.assign(n, 0);

    copy(S, S + n, v.begin());

    int l = -1, r = M;

    while(r - l > 1){
        int m = l + (r - l) / 2;

        v2 = v;

        for(int i = 0; i < m; i++) swap(v2[X[i]], v2[Y[i]]);

        if(swapcnt(v2) <= m) r = m;
        else l = m;
    }   

    v2 = v;

    for(int i = 0; i < r; i++) swap(v2[X[i]], v2[Y[i]]);

    vector<pair<int, int>> swaps;

    vector<bool> vis(n, false);

    for(int i = 0; i < n; i++){
        if(!vis[i]){
            vis[i] = true;

            int x = i;

            while(!vis[v2[x]]){
                vis[x] = true;

                swaps.push_back({v2[x], v2[v2[x]]});

                x = v2[x];
            }
        }
    }

    while(swaps.size() < r) swaps.push_back({0, 0});

    vector<int> where(n);

    for(int i = 0; i < n; i++) where[v[i]] = i;

    for(int i = 0; i < r; i++){
        swap(where[v[X[i]]], where[v[Y[i]]]);
        swap(v[X[i]], v[Y[i]]);

        P[i] = where[swaps[i].first];
        Q[i] = where[swaps[i].second];

        swap(where[v[P[i]]], where[v[Q[i]]]);
        swap(v[P[i]], v[Q[i]]);
    }

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

    return r;
}

Compilation message

sorting.cpp: In function 'int swapcnt(std::vector<int>)':
sorting.cpp:7:28: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
    7 |     int cnt = 0, n = v.size();
      |                      ~~~~~~^~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:73:24: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   73 |     while(swaps.size() < r) swaps.push_back({0, 0});
      |           ~~~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 1 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 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 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 1 ms 340 KB Output is correct
5 Correct 1 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 1 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 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 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 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 440 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 444 KB Output is correct
6 Correct 1 ms 436 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 440 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 1 ms 440 KB Output is correct
14 Correct 1 ms 440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 444 KB Output is correct
6 Correct 1 ms 436 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 440 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 1 ms 440 KB Output is correct
14 Correct 1 ms 440 KB Output is correct
15 Correct 131 ms 10456 KB Output is correct
16 Correct 123 ms 10616 KB Output is correct
17 Correct 143 ms 11128 KB Output is correct
18 Correct 60 ms 7628 KB Output is correct
19 Correct 115 ms 9704 KB Output is correct
20 Correct 127 ms 10100 KB Output is correct
21 Correct 124 ms 10220 KB Output is correct
22 Correct 118 ms 11216 KB Output is correct
23 Correct 137 ms 11504 KB Output is correct
24 Correct 155 ms 11340 KB Output is correct
25 Correct 153 ms 11212 KB Output is correct
26 Correct 123 ms 10088 KB Output is correct
27 Correct 117 ms 9720 KB Output is correct
28 Correct 141 ms 11084 KB Output is correct
29 Correct 150 ms 10860 KB Output is correct
30 Correct 92 ms 9480 KB Output is correct
31 Correct 161 ms 11028 KB Output is correct
32 Correct 135 ms 11252 KB Output is correct
33 Correct 154 ms 11232 KB Output is correct
34 Correct 142 ms 11272 KB Output is correct
35 Correct 116 ms 9748 KB Output is correct
36 Correct 54 ms 8232 KB Output is correct
37 Correct 151 ms 11636 KB Output is correct
38 Correct 143 ms 11184 KB Output is correct
39 Correct 157 ms 11352 KB Output is correct