답안 #885853

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
885853 2023-12-10T21:42:12 Z JakobZorz 정렬하기 (IOI15_sorting) C++17
100 / 100
189 ms 21772 KB
#include"sorting.h"
#include<iostream>
#include<vector>
using namespace std;

int n;
int*x,*y,*s;

vector<pair<int,int>>construct(int m){
    vector<pair<int,int>>ans(m);
    vector<int>perm(n),curr(n),inv_perm(n),inv_curr(n);
    for(int i=0;i<n;i++){
        perm[i]=i;
        inv_perm[i]=i;
        curr[i]=s[i];
        inv_curr[curr[i]]=i;
    }
    
    for(int i=0;i<m;i++){
        swap(curr[x[i]],curr[y[i]]);
        swap(inv_curr[curr[x[i]]],inv_curr[curr[y[i]]]);
    }
    
    for(int i=m-1;i>=0;i--){
        swap(perm[x[i]],perm[y[i]]);
        swap(inv_perm[perm[x[i]]],inv_perm[perm[y[i]]]);
    }
    
    int fi=0;
    for(int i=0;i<m;i++){
        swap(perm[x[i]],perm[y[i]]);
        swap(inv_perm[perm[x[i]]],inv_perm[perm[y[i]]]);
        
        while(fi<n&&curr[fi]==fi)
            fi++;
        
        
        if(fi!=n){
            ans[i].first=inv_perm[fi];
            int k=inv_curr[fi];
            swap(curr[fi],curr[k]);
            swap(inv_curr[curr[fi]],inv_curr[curr[k]]);
            ans[i].second=inv_perm[k];
        }
    }
    
    for(int i=0;i<n;i++)
        if(curr[i]!=i)
            return {};
    
    return ans;
}

int findSwapPairs(int N,int S[],int _,int X[],int Y[],int P[],int Q[]){
    n=N;
    x=X;
    y=Y;
    s=S;
    bool already_done=true;
    for(int i=0;i<n;i++)
        if(s[i]!=i)
            already_done=false;
    if(already_done)
        return 0;
    
    int l=0,r=n;
    while(l<r-1){
        int mid=(l+r)/2;
        if(construct(mid).empty())
            l=mid;
        else
            r=mid;
    }
    int opt=r;
    
    vector<pair<int,int>>res=construct(opt);
    
    for(int i=0;i<opt;i++){
        P[i]=res[i].first;
        Q[i]=res[i].second;
    }
    
	return opt;
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:54:37: warning: unused parameter '_' [-Wunused-parameter]
   54 | int findSwapPairs(int N,int S[],int _,int X[],int Y[],int P[],int Q[]){
      |                                 ~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 456 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 480 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 480 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 135 ms 10344 KB Output is correct
16 Correct 149 ms 18568 KB Output is correct
17 Correct 172 ms 19460 KB Output is correct
18 Correct 35 ms 13396 KB Output is correct
19 Correct 135 ms 18236 KB Output is correct
20 Correct 139 ms 19072 KB Output is correct
21 Correct 140 ms 18948 KB Output is correct
22 Correct 135 ms 14748 KB Output is correct
23 Correct 151 ms 21736 KB Output is correct
24 Correct 181 ms 19880 KB Output is correct
25 Correct 173 ms 19708 KB Output is correct
26 Correct 135 ms 18664 KB Output is correct
27 Correct 125 ms 18260 KB Output is correct
28 Correct 170 ms 20884 KB Output is correct
29 Correct 172 ms 18896 KB Output is correct
30 Correct 107 ms 18292 KB Output is correct
31 Correct 169 ms 19320 KB Output is correct
32 Correct 157 ms 19472 KB Output is correct
33 Correct 179 ms 21440 KB Output is correct
34 Correct 162 ms 21276 KB Output is correct
35 Correct 126 ms 17896 KB Output is correct
36 Correct 59 ms 18216 KB Output is correct
37 Correct 189 ms 21772 KB Output is correct
38 Correct 174 ms 19800 KB Output is correct
39 Correct 177 ms 19884 KB Output is correct