답안 #583811

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
583811 2022-06-26T08:41:56 Z InternetPerson10 정렬하기 (IOI15_sorting) C++17
100 / 100
601 ms 26224 KB
#include "sorting.h"
#include <bits/stdc++.h>
 
using namespace std;
 
bool trySwapPairs(int n, int S[], int m, int X[], int Y[], int P[], int Q[]) {
    vector<int> targetArr(n);
    vector<int> revInd(n);
    int countMatches = 0;
    for(int i = 0; i < n; i++) {
        targetArr[i] = i;
        if(S[i] == i) countMatches++;
    }
    for(int i = m-1; i >= 0; i--) {
        swap(targetArr[X[i]], targetArr[Y[i]]);
    }
    for(int i = 0; i < n; i++) {
        revInd[targetArr[i]] = i;
    }
    vector<int> notGood(n);
    for(int i = 0; i < n; i++) {
        if(S[i] != targetArr[i]) {
            notGood[i] = true;
        }
    }
    vector<int> nums(n);
    vector<int> otherRevInd(n);
    auto swapTwo = [&](int i, int a, int b) {
        if(a == S[a]) countMatches--;
        if(b == S[b]) countMatches--;
        swap(nums[otherRevInd[a]], nums[otherRevInd[b]]);
        swap(otherRevInd[a], otherRevInd[b]);
        swap(S[a], S[b]);
        P[i] = a; Q[i] = b;
        if(a == S[a]) countMatches++;
        if(b == S[b]) countMatches++;
    };
    vector<int> taken(n, false);
    vector<pair<int, int>> pairs;
    pairs.clear();
    for(int i = 0; i < n; i++) {
        taken[i] = false;
    }
    vector<int> v;
    for(int i = 0; i < n; i++) {
        otherRevInd[i] = i;
        nums[i] = i;
        if(taken[i]) continue;
        taken[i] = true;
        v.push_back(i);
        int x = revInd[S[i]];
        while(x != i) {
            taken[x] = true;
            v.push_back(x);
            x = revInd[S[x]];
        }
        for(int j = 1; j < v.size(); j++) {
            pairs.push_back({v[j-1], v[j]});
        }
        v.clear();
    }
    while(pairs.size() < m) pairs.push_back({0, 0});
    for(int i = 0; i < m; i++) {
        if(countMatches == n) {
            m = i;
            break;
        }
        if(X[i] != Y[i]) {
            if(X[i] == S[X[i]]) countMatches--;
            if(Y[i] == S[Y[i]]) countMatches--;
            swap(nums[otherRevInd[X[i]]], nums[otherRevInd[Y[i]]]);
            swap(otherRevInd[X[i]], otherRevInd[Y[i]]);
            swap(S[X[i]], S[Y[i]]);
            if(X[i] == S[X[i]]) countMatches++;
            if(Y[i] == S[Y[i]]) countMatches++;
        }
        swapTwo(i, nums[pairs[i].first], nums[pairs[i].second]);
        /*
        cout << "Current state: ";
        for(int j = 0; j < n; j++) cout << S[j] << ' ';
        cout << '\n';
        */
    }
    return (countMatches == n);
}
 
int findSwapPairs(int n, int R[], int m, int X[], int Y[], int P[], int Q[]) {
    int V[200001];
    int l = -1, r = min(m, n+1);
    while(l != r - 1) {
        for(int i = 0; i < n; i++) {
            V[i] = R[i];
        }
        int mid = (l + r + 1) / 2;
        if(trySwapPairs(n, V, mid, X, Y, P, Q)) r = mid;
        else l = mid;
    }
    trySwapPairs(n, R, r, X, Y, P, Q);
    return r;
}

Compilation message

sorting.cpp: In function 'bool trySwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:57:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         for(int j = 1; j < v.size(); j++) {
      |                        ~~^~~~~~~~~~
sorting.cpp:62: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]
   62 |     while(pairs.size() < m) pairs.push_back({0, 0});
      |           ~~~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 1 ms 968 KB Output is correct
9 Correct 1 ms 980 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 1 ms 968 KB Output is correct
9 Correct 1 ms 980 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 1 ms 1108 KB Output is correct
16 Correct 1 ms 1108 KB Output is correct
17 Correct 1 ms 1108 KB Output is correct
18 Correct 1 ms 980 KB Output is correct
19 Correct 1 ms 980 KB Output is correct
20 Correct 1 ms 980 KB Output is correct
21 Correct 2 ms 1236 KB Output is correct
22 Correct 2 ms 1320 KB Output is correct
23 Correct 2 ms 1236 KB Output is correct
24 Correct 2 ms 1236 KB Output is correct
25 Correct 2 ms 1336 KB Output is correct
26 Correct 2 ms 1236 KB Output is correct
27 Correct 3 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1236 KB Output is correct
2 Correct 3 ms 1236 KB Output is correct
3 Correct 3 ms 1236 KB Output is correct
4 Correct 2 ms 1148 KB Output is correct
5 Correct 2 ms 1272 KB Output is correct
6 Correct 2 ms 1208 KB Output is correct
7 Correct 3 ms 1236 KB Output is correct
8 Correct 3 ms 1236 KB Output is correct
9 Correct 3 ms 1208 KB Output is correct
10 Correct 3 ms 1300 KB Output is correct
11 Correct 3 ms 1236 KB Output is correct
12 Correct 3 ms 1236 KB Output is correct
13 Correct 3 ms 1264 KB Output is correct
14 Correct 2 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1236 KB Output is correct
2 Correct 3 ms 1236 KB Output is correct
3 Correct 3 ms 1236 KB Output is correct
4 Correct 2 ms 1148 KB Output is correct
5 Correct 2 ms 1272 KB Output is correct
6 Correct 2 ms 1208 KB Output is correct
7 Correct 3 ms 1236 KB Output is correct
8 Correct 3 ms 1236 KB Output is correct
9 Correct 3 ms 1208 KB Output is correct
10 Correct 3 ms 1300 KB Output is correct
11 Correct 3 ms 1236 KB Output is correct
12 Correct 3 ms 1236 KB Output is correct
13 Correct 3 ms 1264 KB Output is correct
14 Correct 2 ms 1236 KB Output is correct
15 Correct 522 ms 15992 KB Output is correct
16 Correct 601 ms 24084 KB Output is correct
17 Correct 593 ms 24900 KB Output is correct
18 Correct 116 ms 19700 KB Output is correct
19 Correct 390 ms 22288 KB Output is correct
20 Correct 447 ms 24784 KB Output is correct
21 Correct 423 ms 24864 KB Output is correct
22 Correct 462 ms 19972 KB Output is correct
23 Correct 561 ms 26200 KB Output is correct
24 Correct 586 ms 25976 KB Output is correct
25 Correct 577 ms 25192 KB Output is correct
26 Correct 426 ms 23980 KB Output is correct
27 Correct 355 ms 22364 KB Output is correct
28 Correct 572 ms 25132 KB Output is correct
29 Correct 528 ms 24192 KB Output is correct
30 Correct 295 ms 22204 KB Output is correct
31 Correct 546 ms 24624 KB Output is correct
32 Correct 539 ms 25264 KB Output is correct
33 Correct 581 ms 25268 KB Output is correct
34 Correct 589 ms 24724 KB Output is correct
35 Correct 346 ms 21924 KB Output is correct
36 Correct 127 ms 20904 KB Output is correct
37 Correct 550 ms 26224 KB Output is correct
38 Correct 566 ms 25152 KB Output is correct
39 Correct 561 ms 25352 KB Output is correct