답안 #405886

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
405886 2021-05-17T02:38:46 Z jk410 정렬하기 (IOI15_sorting) C++17
100 / 100
199 ms 22148 KB
#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
int A[200000],tmp[200000],tmp_P[200000],tmp_Q[200000];
bool Visited[200000];
bool f(int N,int S[],int X[],int Y[],int k){
    for (int i=0; i<N; i++){
        tmp[i]=S[i];
        Visited[i]=false;
    }
    for (int i=0; i<k; i++){
        swap(tmp[X[i]],tmp[Y[i]]);
        tmp_P[i]=tmp_Q[i]=0;
    }
    int cnt=0;
    for (int i=0; i<N; i++){
        if (tmp[i]!=i&&!Visited[i]){
            Visited[i]=true;
            for (int j=tmp[i]; !Visited[j]; j=tmp[j]){
                Visited[j]=true;
                tmp_P[cnt]=j;
                tmp_Q[cnt]=tmp[j];
                cnt++;
                if (cnt>k)
                    return false;
            }
        }
    }
    return true;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    int ans=0,l=0,r=N;
    for (int i=0; i<N; i++)
        A[S[i]]=i;
    while (l<=r){
        int m=(l+r)>>1;
        if (f(N,S,X,Y,m)){
            ans=m;
            r=m-1;
        }
        else
            l=m+1;
    }
    f(N,S,X,Y,ans);
    for (int i=0; i<ans; i++){
        swap(A[S[X[i]]],A[S[Y[i]]]);
        swap(S[X[i]],S[Y[i]]);
        P[i]=A[tmp_P[i]];
        Q[i]=A[tmp_Q[i]];
        swap(S[A[tmp_P[i]]],S[A[tmp_Q[i]]]);
        swap(A[tmp_P[i]],A[tmp_Q[i]]);
    }
    return ans;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:31:39: warning: unused parameter 'M' [-Wunused-parameter]
   31 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 288 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 356 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 1 ms 428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 1 ms 428 KB Output is correct
15 Correct 134 ms 10948 KB Output is correct
16 Correct 150 ms 19108 KB Output is correct
17 Correct 169 ms 20544 KB Output is correct
18 Correct 63 ms 15760 KB Output is correct
19 Correct 137 ms 18528 KB Output is correct
20 Correct 161 ms 19608 KB Output is correct
21 Correct 145 ms 19652 KB Output is correct
22 Correct 124 ms 15448 KB Output is correct
23 Correct 165 ms 20860 KB Output is correct
24 Correct 163 ms 20752 KB Output is correct
25 Correct 161 ms 20932 KB Output is correct
26 Correct 136 ms 19024 KB Output is correct
27 Correct 136 ms 18464 KB Output is correct
28 Correct 163 ms 20420 KB Output is correct
29 Correct 187 ms 20352 KB Output is correct
30 Correct 98 ms 17860 KB Output is correct
31 Correct 163 ms 20912 KB Output is correct
32 Correct 155 ms 20088 KB Output is correct
33 Correct 170 ms 20992 KB Output is correct
34 Correct 162 ms 20672 KB Output is correct
35 Correct 147 ms 18372 KB Output is correct
36 Correct 57 ms 16836 KB Output is correct
37 Correct 181 ms 22148 KB Output is correct
38 Correct 199 ms 20800 KB Output is correct
39 Correct 176 ms 21276 KB Output is correct