답안 #299476

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
299476 2020-09-15T02:36:46 Z Hideo 정렬하기 (IOI15_sorting) C++17
100 / 100
268 ms 31792 KB
#include "sorting.h"
//#include "grader.cpp"
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define all(s) s.begin(), s.end()
#define pb push_back
#define fr first
#define sc second
#define vi vector < int >
#define pi pair < int, int >

const int MN = 6e5 + 7;

vector < pi > sw, out;
int a[MN], b[MN], r[MN];
int x[MN], y[MN];
int n, m;

bool check (int k){
    for (int i = 0; i < n; i++)
        a[i] = b[i];
    for (int i = 0; i < k; i++)
        swap(a[x[i]], a[y[i]]);
    for (int i = 0; i < n; i++)
        r[a[i]] = i;
    vector < pi > kek;
    for (int i = 0; i < n; i++){
        int it = i;
        while (it != a[it]){
            int x = it, y = a[it];
            it = a[it];
            kek.pb({x, y});
            swap(a[r[x]], a[r[y]]);
            swap(r[x], r[y]);
        }
    }
    for (int i = 0; i < n; i++){
        int it = i;
        while (it != a[it]){
            int x = it, y = a[it];
            it = a[it];
            kek.pb({x, y});
            swap(a[r[x]], a[r[y]]);
            swap(r[x], r[y]);
        }
    }
    if (kek.size() <= k)
        sw = kek;
    return kek.size() <= k;
}

void simulate (int k){
    while (sw.size() != k)
        sw.pb({0, 0});
    for (int i = 0; i < n; i++){
        a[i] = b[i];
        r[a[i]] = i;
    }
    for (int i = 0; i < k; i++){
        swap(r[a[x[i]]], r[a[y[i]]]);
        swap(a[x[i]], a[y[i]]);
        int v = r[sw[i].fr], u = r[sw[i].sc];
        out.pb({v, u});
        swap(r[a[v]], r[a[u]]);
        swap(a[v], a[u]);
    }
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    n = N; m = M;
    bool sorted = true;
    for (int i = 0; i < n; i++){
        b[i] = S[i];
        if (b[i] != i)
            sorted = false;
    }
    if (sorted)
        return 0;
    for (int i = 0; i < m; i++){
        x[i] = X[i];
        y[i] = Y[i];
    }
    int lf = 0, rg = N + 1;
    while (lf + 1 < rg){
        int mid = (lf + rg) >> 1;
        if (check(mid))
            rg = mid;
        else
            lf = mid;
    }
    simulate(rg);
    for (int i = 0; i < out.size(); i++){
        P[i] = out[i].fr;
        Q[i] = out[i].sc;
    }
    return rg;
}

Compilation message

sorting.cpp: In function 'bool check(int)':
sorting.cpp:33:17: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   33 |             int x = it, y = a[it];
      |                 ^
sorting.cpp:19:5: note: shadowed declaration is here
   19 | int x[MN], y[MN];
      |     ^
sorting.cpp:33:25: warning: declaration of 'y' shadows a global declaration [-Wshadow]
   33 |             int x = it, y = a[it];
      |                         ^
sorting.cpp:19:12: note: shadowed declaration is here
   19 | int x[MN], y[MN];
      |            ^
sorting.cpp:43:17: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   43 |             int x = it, y = a[it];
      |                 ^
sorting.cpp:19:5: note: shadowed declaration is here
   19 | int x[MN], y[MN];
      |     ^
sorting.cpp:43:25: warning: declaration of 'y' shadows a global declaration [-Wshadow]
   43 |             int x = it, y = a[it];
      |                         ^
sorting.cpp:19:12: note: shadowed declaration is here
   19 | int x[MN], y[MN];
      |            ^
sorting.cpp:50:20: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   50 |     if (kek.size() <= k)
      |         ~~~~~~~~~~~^~~~
sorting.cpp:52:23: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   52 |     return kek.size() <= k;
      |            ~~~~~~~~~~~^~~~
sorting.cpp: In function 'void simulate(int)':
sorting.cpp:56:22: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   56 |     while (sw.size() != k)
      |            ~~~~~~~~~~^~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:95:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |     for (int i = 0; i < out.size(); i++){
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 640 KB Output is correct
22 Correct 1 ms 640 KB Output is correct
23 Correct 2 ms 640 KB Output is correct
24 Correct 1 ms 640 KB Output is correct
25 Correct 2 ms 640 KB Output is correct
26 Correct 1 ms 640 KB Output is correct
27 Correct 1 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 213 ms 19896 KB Output is correct
16 Correct 229 ms 28588 KB Output is correct
17 Correct 265 ms 29584 KB Output is correct
18 Correct 45 ms 14200 KB Output is correct
19 Correct 169 ms 25536 KB Output is correct
20 Correct 167 ms 27128 KB Output is correct
21 Correct 173 ms 27000 KB Output is correct
22 Correct 219 ms 25672 KB Output is correct
23 Correct 247 ms 30732 KB Output is correct
24 Correct 255 ms 30140 KB Output is correct
25 Correct 251 ms 29816 KB Output is correct
26 Correct 166 ms 26412 KB Output is correct
27 Correct 164 ms 26008 KB Output is correct
28 Correct 231 ms 31792 KB Output is correct
29 Correct 238 ms 28908 KB Output is correct
30 Correct 134 ms 23840 KB Output is correct
31 Correct 238 ms 29768 KB Output is correct
32 Correct 236 ms 29996 KB Output is correct
33 Correct 268 ms 30212 KB Output is correct
34 Correct 261 ms 30068 KB Output is correct
35 Correct 170 ms 25312 KB Output is correct
36 Correct 77 ms 21368 KB Output is correct
37 Correct 258 ms 30408 KB Output is correct
38 Correct 250 ms 29696 KB Output is correct
39 Correct 258 ms 29772 KB Output is correct