답안 #669765

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
669765 2022-12-07T09:01:01 Z alvingogo 정렬하기 (IOI15_sorting) C++14
0 / 100
575 ms 28812 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
using namespace std;

int n;
void print(vector<int> s){
    for(int i=0;i<n;i++){
        cout << s[i] << " ";
    }
    cout << "\n";
}
void print2(int s[]){
    for(int i=0;i<n;i++){
        cout << s[i] << " ";
    }
    cout << "\n";
}
int findSwapPairs(int N,int S[],int M,int X[],int Y[],int P[],int Q[]){
    int l=0,r=M;
    n=N;
    while(r>l){
        int m=(l+r)/2;
        vector<int> v(n);
        iota(v.begin(),v.end(),0);
        for(int i=0;i<m;i++){
            swap(v[X[i]],v[Y[i]]);
        }
        vector<int> g(n);
        for(int i=0;i<n;i++){
            g[v[i]]=i;
        }
        v=g;
        for(int i=0;i<n;i++){
            g[v[i]]=S[i];
        }
        vector<int> vis(n);
        int cnt=0;
        for(int i=0;i<n;i++){
            if(!vis[i]){
                cnt++;
                vis[i]=1;
                int a=i;
                while(1){
                    a=g[a];
                    if(vis[a]){
                        break;
                    }
                    vis[a]=1;
                }
            }
        }
        int t=n-cnt;
        if(t<=m){
            r=m;
        }
        else{
            l=m+1;
        }
    }
    vector<int> v(n);
    iota(v.begin(),v.end(),0);
    for(int i=0;i<l;i++){
        swap(v[X[i]],v[Y[i]]);
    }
    vector<int> g(n);
    for(int i=0;i<n;i++){
        g[v[i]]=i;
    }
    v=g;
    vector<int> d(n),e(n);
    for(int i=0;i<n;i++){
        d[S[i]]=i;
        e[v[i]]=i;
    }
    vector<int> vis(n);
    int cnt=0;
    int c=0,cn=0;
    for(int i=0;i<l;i++){
        {
            int t=X[i],f=Y[i];
            swap(d[S[t]],d[S[f]]);
            swap(S[t],S[f]);
            swap(e[v[t]],e[v[f]]);
            swap(v[t],v[f]);
        }
        while(cn<n){
            if(v[cn]!=S[cn]){
                int t=cn,f=d[v[cn]];
                swap(d[S[t]],d[S[f]]);
                swap(S[t],S[f]);
                P[c]=t;
                Q[c]=f;
                c++;             
                cn++;
                break;
            }
            cn++;
        }
        print2(S);
        print(v);
    }
    if(c<l){
        P[c]=Q[c]=0;
        c++;
    }
    return l;
}
/*
int main(){
    int N, M;
    cin >> N;
	int *S = (int*)malloc(sizeof(int) * (unsigned int)N);
	for (int i = 0; i < N; ++i)
		cin >> S[i];
	cin >> M;
	int *X = (int*)malloc(sizeof(int) * (unsigned int)M), *Y = (int*)malloc(sizeof(int) * (unsigned int)M);
	for (int i = 0; i < M; ++i) {
	    cin >> X[i];
        cin >> Y[i];
	}
	int *P = (int*)malloc(sizeof(int) * (unsigned int)M), *Q = (int*)malloc(sizeof(int) * (unsigned int)M);
	int ans = findSwapPairs(N, S, M, X, Y, P, Q);
	cout << ans << "\n";
	for (int i = 0; i < ans; ++i)
		cout << P[i] << " " << Q[i] << "\n";
    return 0;
}
*/

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:80:9: warning: unused variable 'cnt' [-Wunused-variable]
   80 |     int cnt=0;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Expected EOLN
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Expected EOLN
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Expected EOLN
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Expected EOLN
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 575 ms 28812 KB Expected EOLN
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 575 ms 28812 KB Expected EOLN
2 Halted 0 ms 0 KB -