답안 #804356

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
804356 2023-08-03T08:13:26 Z _martynas 정렬하기 (IOI15_sorting) C++11
100 / 100
193 ms 28876 KB
#include "sorting.h"
#include <bits/stdc++.h>

using namespace std;

int tryFindSwapPairs(const int N, int S[], const int M, int X[], int Y[], int P[], int Q[]) {
    for (int i = 0; i < M; i++) P[i] = Q[i] = 0;
    int T[N];
    for (int i = 0; i < N; i++) T[i] = S[i];
    for (int i = 0; i < M; i++) swap(T[X[i]], T[Y[i]]);
    int where[N];
    for (int i = 0; i < N; i++) where[T[i]] = i;
    vector<pair<int, int>> swap_list;
    for (int i = 0; i < N; i++) {
        if (T[i] != i) {
            swap_list.emplace_back(i, T[i]);
            int a = i, b = where[i];
            swap(where[T[a]], where[T[b]]);
            swap(T[a], T[b]);
        }
    }
    for (int i = 0; i < N; i++) T[i] = S[i];
    for (int i = 0; i < N; i++) where[T[i]] = i;
    for (int i = 0; i < (int)swap_list.size(); i++) {
        swap(where[T[X[i]]], where[T[Y[i]]]);
        swap(T[X[i]], T[Y[i]]);
        int a = where[swap_list[i].first], b = where[swap_list[i].second];
        P[i] = a;
        Q[i] = b;
        swap(where[T[a]], where[T[b]]);
        swap(T[a], T[b]);
    }
    for (int i = 0; i < N; i++) T[i] = S[i];
    for (int i = 0; i < M; i++) {
        swap(T[X[i]], T[Y[i]]);
        swap(T[P[i]], T[Q[i]]);
    }
    if (!is_sorted(T, T+N)) return -1;
    return M;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    int l = 0, r = N;
    while (l < r) {
        int mid = (l+r)/2;
        int p[M], q[M];
        int ans = tryFindSwapPairs(N, S, mid, X, Y, p, q);
        if (ans == -1) {
            l = mid+1;
        }
        else {
            r = mid;
        }
    }
    int p[M], q[M];
    int ans = tryFindSwapPairs(N, S, l, X, Y, p, q);
    copy(p, p+M, P), copy(q, q+M, Q);
    return ans;
}

// int main(int argc, char *argv[]) {
//     chrono::time_point<chrono::steady_clock> ts = chrono::steady_clock::now();
//     if (argc > 1) srand(atoi(argv[1]));
//     int N = rand() % 2000000 + 1;
//     int M = 3*N;
//     int S[N], X[M], Y[M], P[M], Q[M];
//     iota(S, S+N, 0);
//     random_shuffle(S, S+N);
//     // for (int i = 0; i < N; i++) {
//     //     cerr << S[i] << " \n"[i == N-1];
//     // }
//     for (int i = 0; i < M; i++) {
//         int a = rand() % N, b = rand() % N;
//         if (a > b) swap(a, b);
//         X[i] = a, Y[i] = b;
//     }
//     int cnt = findSwapPairs(N, S, M, X, Y, P, Q);
//     for (int i = 0; i < cnt; i++) {
//         swap(S[X[i]], S[Y[i]]);
//         swap(S[P[i]], S[Q[i]]);
//     }
//     assert(is_sorted(S, S+N));
//     cerr << "Exec: " << chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now()-ts).count() << "ms\n";
//     return 0;
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 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 0 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 280 KB Output is correct
6 Correct 0 ms 212 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 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 0 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 280 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 596 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 1 ms 596 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 440 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 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 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 468 KB Output is correct
14 Correct 1 ms 340 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 440 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 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 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 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 134 ms 17308 KB Output is correct
16 Correct 144 ms 25492 KB Output is correct
17 Correct 193 ms 26868 KB Output is correct
18 Correct 60 ms 24520 KB Output is correct
19 Correct 119 ms 26172 KB Output is correct
20 Correct 141 ms 28744 KB Output is correct
21 Correct 126 ms 28876 KB Output is correct
22 Correct 136 ms 22296 KB Output is correct
23 Correct 150 ms 27848 KB Output is correct
24 Correct 179 ms 27452 KB Output is correct
25 Correct 176 ms 27068 KB Output is correct
26 Correct 136 ms 28752 KB Output is correct
27 Correct 123 ms 26288 KB Output is correct
28 Correct 185 ms 27348 KB Output is correct
29 Correct 169 ms 26872 KB Output is correct
30 Correct 98 ms 26688 KB Output is correct
31 Correct 167 ms 27504 KB Output is correct
32 Correct 149 ms 26852 KB Output is correct
33 Correct 182 ms 27456 KB Output is correct
34 Correct 181 ms 27292 KB Output is correct
35 Correct 120 ms 25820 KB Output is correct
36 Correct 51 ms 25152 KB Output is correct
37 Correct 180 ms 28168 KB Output is correct
38 Correct 165 ms 26996 KB Output is correct
39 Correct 175 ms 27284 KB Output is correct