답안 #601951

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
601951 2022-07-22T12:53:34 Z PiejanVDC 정렬하기 (IOI15_sorting) C++17
54 / 100
20 ms 724 KB
//#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;

int findSwapPairs(int n, int s[], int m, int X[], int Y[], int P[], int Q[]) {
    auto sorted = [&] (vector<int>&v) -> bool {
        for(int i = 0 ; i < n ; i++)
            if(v[i] != i)
                return 0;
        return 1;
    };
    int ans;
    int l = 0, r = m;
    while(l <= r) {
        int T = (l+r)/2;
        //cout << "T : " << T << '\n';
        vector<int>c(n);
        for(int i = 0 ; i < n ; i++) c[i]= i;
        for(int i = 0 ; i < T ; i++)
            swap(c[X[i]], c[Y[i]]);
        vector<int>go(n, -1);
        for(int i = 0 ; i < n ; i++) {
            //assert(go[c[i]] == -1);
            go[c[i]] = i;
        }
        vector<int>v(n);
        auto S = [&] (int W) {
            for(int i = 0 ; i < n ; i++)
                if(v[i] == W)
                    return i;
            //assert(0);
        };
        vector<int>P_(m), Q_(m);
        for(int i = 0 ; i < n ; i++) v[i] = s[i];
        int cnt = 0;
        set<int>s;
        for(int i = 0 ; i < n ; i++) s.insert(i);
        for(int i = 0 ; i < T ; i++) {
            cnt++;
            swap(v[X[i]], v[Y[i]]);
            swap(go[X[i]], go[Y[i]]);
            int j = n;
            bool ok = 1;
            while(!s.empty()) {
                j = *s.begin();
                s.erase(s.begin());
                if(go[j] != v[j]) {
                    int p = S(go[j]);
                    P_[cnt-1] = p;
                    Q_[cnt-1] = j;
                    swap(v[p], v[j]);
                    s.insert(p);
                    s.insert(j);
                    ok = 0;
                    break;
                }
            }
            /*cout << "    " << i << '\n';
            cout << "    ";
            for(auto z : v)
                cout << z << ' ';
            cout << '\n';*/
            if(ok)
                P_[cnt-1] = 0, Q_[cnt-1] = 0;
            // do the swappings
            // look for first element not correct in go
        }
        if(sorted(v)) {
            r = T-1;
            for(int i_ = 0 ; i_ < cnt ; i_++)
                P[i_] = P_[i_], Q[i_] = Q_[i_];
            ans = cnt;
        } else l = T+1;
    }
   return ans;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:36:17: warning: declaration of 'std::set<int> s' shadows a parameter [-Wshadow]
   36 |         set<int>s;
      |                 ^
sorting.cpp:5:30: note: shadowed declaration is here
    5 | int findSwapPairs(int n, int s[], int m, int X[], int Y[], int P[], int Q[]) {
      |                          ~~~~^~~
sorting.cpp: In lambda function:
sorting.cpp:32:9: warning: control reaches end of non-void function [-Wreturn-type]
   32 |         };
      |         ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:75:11: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   75 |    return ans;
      |           ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 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 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 256 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 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 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 1 ms 340 KB Output is correct
4 Correct 1 ms 360 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 1 ms 256 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 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 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 1 ms 340 KB Output is correct
16 Correct 1 ms 360 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 4 ms 596 KB Output is correct
22 Correct 4 ms 612 KB Output is correct
23 Correct 5 ms 596 KB Output is correct
24 Correct 7 ms 596 KB Output is correct
25 Correct 3 ms 596 KB Output is correct
26 Correct 5 ms 596 KB Output is correct
27 Correct 8 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 468 KB Output is correct
2 Correct 19 ms 540 KB Output is correct
3 Correct 17 ms 528 KB Output is correct
4 Incorrect 20 ms 556 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 468 KB Output is correct
2 Correct 19 ms 540 KB Output is correct
3 Correct 17 ms 528 KB Output is correct
4 Incorrect 20 ms 556 KB Output isn't correct
5 Halted 0 ms 0 KB -