답안 #594610

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
594610 2022-07-12T17:50:17 Z davi_bart 정렬하기 (IOI15_sorting) C++14
100 / 100
181 ms 22576 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>

#include "sorting.h"
using namespace std;
#define ll long long
// #define int ll
#define fi first
#define se second
#define ld long double
#define pb push_back
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int N;
vector<int> v;
vector<int> ini;
vector<pair<int, int>> scambi;
vector<bool> vis(200010);
void dfs(int pos, int ini) {
    vis[pos] = 1;
    scambi.pb({pos, v[pos]});
    if (v[pos] == ini) return;
    dfs(v[pos], ini);
}
int findSwapPairs(int N, int S[], int M, int P[], int Q[], int X[], int Y[]) {
    ::N = N;
    for (int i = 0; i < N; i++) v.pb(S[i]);
    ini = v;
    int ans = 0;
    int l = 0, r = N + 2;
    while (l < r) {
        int m = (l + r) / 2;
        v = ini;
        fill(vis.begin(), vis.begin() + N + 10, 0);
        scambi.clear();
        for (int i = 0; i < m; i++) {
            swap(v[P[i]], v[Q[i]]);
        }
        for (int j = 0; j < N; j++) {
            if (vis[j] || v[j] == j) continue;
            dfs(v[j], j);
        }
        if (scambi.size() <= m)
            r = m;
        else
            l = m + 1;
    }
    ans = l;
    v = ini;
    fill(vis.begin(), vis.begin() + N + 10, 0);
    scambi.clear();
    for (int i = 0; i < l; i++) {
        swap(v[P[i]], v[Q[i]]);
    }

    for (int j = 0; j < N; j++) {
        if (vis[j] || v[j] == j) continue;
        dfs(v[j], j);
    }
    v = ini;
    vector<int> pos(N);

    for (int j = 0; j < N; j++) {
        pos[v[j]] = j;
    }
    for (int i = 0; i < ans; i++) {
        swap(v[P[i]], v[Q[i]]);
        swap(pos[v[P[i]]], pos[v[Q[i]]]);

        if (i >= scambi.size()) {
            X[i] = 0;
            Y[i] = 0;
            continue;
        }
        X[i] = pos[scambi[i].fi];
        Y[i] = pos[scambi[i].se];
        // int k = pos[v[X[i]]];
        swap(v[X[i]], v[Y[i]]);
        swap(pos[v[X[i]]], pos[v[Y[i]]]);
    }
    return ans;
}

Compilation message

sorting.cpp: In function 'void dfs(int, int)':
sorting.cpp:18:23: warning: declaration of 'ini' shadows a global declaration [-Wshadow]
   18 | void dfs(int pos, int ini) {
      |                   ~~~~^~~
sorting.cpp:15:13: note: shadowed declaration is here
   15 | vector<int> ini;
      |             ^~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:24:23: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   24 | int findSwapPairs(int N, int S[], int M, int P[], int Q[], int X[], int Y[]) {
      |                   ~~~~^
sorting.cpp:13:5: note: shadowed declaration is here
   13 | int N;
      |     ^
sorting.cpp:42:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   42 |         if (scambi.size() <= m)
      |             ~~~~~~~~~~~~~~^~~~
sorting.cpp:69:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |         if (i >= scambi.size()) {
      |             ~~^~~~~~~~~~~~~~~~
sorting.cpp:24:39: warning: unused parameter 'M' [-Wunused-parameter]
   24 | int findSwapPairs(int N, int S[], int M, int P[], int Q[], int X[], int Y[]) {
      |                                   ~~~~^
# 결과 실행 시간 메모리 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 1 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 1 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 1 ms 212 KB Output is correct
9 Correct 1 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 0 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 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 1 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 1 ms 212 KB Output is correct
9 Correct 1 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 0 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 0 ms 212 KB Output is correct
20 Correct 0 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 1 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 340 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 128 ms 11580 KB Output is correct
16 Correct 137 ms 19556 KB Output is correct
17 Correct 165 ms 21144 KB Output is correct
18 Correct 59 ms 16360 KB Output is correct
19 Correct 130 ms 18512 KB Output is correct
20 Correct 134 ms 20164 KB Output is correct
21 Correct 151 ms 20216 KB Output is correct
22 Correct 130 ms 16012 KB Output is correct
23 Correct 164 ms 21496 KB Output is correct
24 Correct 154 ms 21332 KB Output is correct
25 Correct 155 ms 21520 KB Output is correct
26 Correct 148 ms 20040 KB Output is correct
27 Correct 117 ms 18484 KB Output is correct
28 Correct 146 ms 20932 KB Output is correct
29 Correct 144 ms 21060 KB Output is correct
30 Correct 100 ms 17732 KB Output is correct
31 Correct 154 ms 21648 KB Output is correct
32 Correct 141 ms 20600 KB Output is correct
33 Correct 153 ms 21564 KB Output is correct
34 Correct 169 ms 21344 KB Output is correct
35 Correct 120 ms 18356 KB Output is correct
36 Correct 66 ms 16828 KB Output is correct
37 Correct 156 ms 22576 KB Output is correct
38 Correct 181 ms 21376 KB Output is correct
39 Correct 152 ms 22012 KB Output is correct