답안 #885850

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
885850 2023-12-10T21:25:06 Z JakobZorz 정렬하기 (IOI15_sorting) C++17
54 / 100
5 ms 604 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);
    for(int i=0;i<n;i++){
        perm[i]=i;
        inv_perm[i]=i;
        curr[i]=s[i];
    }
    
    for(int i=0;i<m;i++)
        swap(curr[x[i]],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]]]);
    }
    
    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]]]);
        
        for(int j=0;j<n;j++){
            if(curr[j]!=j){
                ans[i].first=inv_perm[j];
                for(int k=0;k<n;k++){
                    if(curr[k]==j){
                        swap(curr[j],curr[k]);
                        ans[i].second=inv_perm[k];
                        break;
                    }
                }
                break;
            }
        }
    }
    
    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;
    
    vector<pair<int,int>>res=construct(n);
    for(int i=0;i<n;i++){
        P[i]=res[i].first;
        Q[i]=res[i].second;
    }
    
    vector<int>test(n);
    for(int i=0;i<n;i++)
        test[i]=s[i];
    
    for(int i=0;i<n;i++){
        swap(test[x[i]],test[y[i]]);
        swap(test[P[i]],test[Q[i]]);
        bool sorted=true;
        for(int j=0;j<n;j++)
            if(test[j]!=j)
                sorted=false;
        if(sorted)
            return i+1;
    }
    
#ifdef JAKOB
    for(int i:test)
        cout<<i<<" ";
    cout<<"\n";
#endif
	return n;
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:48:37: warning: unused parameter '_' [-Wunused-parameter]
   48 | 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 348 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 348 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 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 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 388 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 348 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 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 388 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 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -