답안 #669719

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
669719 2022-12-07T07:00:37 Z victor_gao 정렬하기 (IOI15_sorting) C++17
0 / 100
121 ms 784 KB
#include "sorting.h"
#include <bits/stdc++.h>
#define MAXN 200015
using namespace std;
int bk[MAXN],pos_bk[MAXN],pre[MAXN],pos_pre[MAXN];
int solve(int N,int n,int S[],int P[],int Q[],int X[],int Y[]){
    int now=0,cnt_same=0;
    for (int i=0;i<N;i++){
        bk[i]=i; pos_bk[i]=i;
        pre[i]=S[i];  pos_pre[S[i]]=i;
        if (pre[i]==i) cnt_same++;
    }
    for (int i=n-1;i>=0;i--){
        swap(bk[X[i]],bk[Y[i]]);
        pos_bk[bk[X[i]]]=X[i];
        pos_bk[bk[Y[i]]]=Y[i];
    }
    for (int i=0;i<n;i++){
        if (cnt_same==N) return i;
        if (pre[X[i]]==X[i]) cnt_same--;
        if (pre[Y[i]]==Y[i]) cnt_same--;
        swap(pre[X[i]],pre[Y[i]]);
        if (pre[X[i]]==X[i]) cnt_same++;
        if (pre[Y[i]]==Y[i]) cnt_same++;
        pos_pre[pre[X[i]]]=X[i];
        pos_pre[pre[Y[i]]]=Y[i];
        swap(bk[X[i]],bk[Y[i]]);
        pos_bk[bk[X[i]]]=X[i];
        pos_bk[bk[Y[i]]]=Y[i];
        now=i;
        while (now<N&&pos_pre[now]==pos_bk[now])
            now++;
        if (now>=N) continue;
        P[i]=pos_pre[now]; Q[i]=pos_bk[now];
        if (pre[P[i]]==P[i]) cnt_same--;
        if (pre[Q[i]]==Q[i]) cnt_same--;
        swap(pre[pos_pre[now]],pre[pos_bk[now]]);
        if (pre[P[i]]==P[i]) cnt_same++;
        if (pre[Q[i]]==Q[i]) cnt_same++;
        pos_pre[pre[pos_pre[now]]]=pos_pre[now];
        pos_pre[pre[pos_bk[now]]]=pos_bk[now];
    }
    bool flag=1;
    for (int i=0;i<N;i++){
        if (pre[i]!=i) flag=0;
    }
    if (flag) return n;
    else return 1e9;
}
int solve1(int N,int n,int S[],int P[],int Q[],int X[],int Y[]){
    for (int i=0;i<N;i++){
        bk[i]=i; pos_bk[i]=i;
        pre[i]=S[i];  pos_pre[S[i]]=i;
    }
    for (int i=n-1;i>=0;i--){
        swap(bk[X[i]],bk[Y[i]]);
        pos_bk[bk[X[i]]]=X[i];
        pos_bk[bk[Y[i]]]=Y[i];
    }
    int now=0;
    for (int i=0;i<n;i++){
        bool flag=1;
        for (int j=0;j<N;j++){
            if (pre[j]!=j){
                flag=0;
                break;
            } 
        }
        if (flag)
            return i;
        swap(pre[X[i]],pre[Y[i]]);
        pos_pre[pre[X[i]]]=X[i];
        pos_pre[pre[Y[i]]]=Y[i];
        swap(bk[X[i]],bk[Y[i]]);
        pos_bk[bk[X[i]]]=X[i];
        pos_bk[bk[Y[i]]]=Y[i];
        now=i;
        while (now<N&&pos_pre[now]==pos_bk[now])
            now++;
        if (now>=N) continue;
        P[i]=pos_pre[now]; Q[i]=pos_bk[now];
        swap(pre[pos_pre[now]],pre[pos_bk[now]]);
        pos_pre[pre[pos_pre[now]]]=pos_pre[now];
        pos_pre[pre[pos_bk[now]]]=pos_bk[now];
    }
    bool flag=1;
    for (int i=0;i<N;i++){
        if (pre[i]!=i) flag=0;
    }
    if (flag) return n;
    else return 1e9;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    /*
    int l=0,r=M;
    while (l<r){
        int mid=(l+r)>>1;
        if (solve(N,mid,S,P,Q,X,Y)<1e8) r=mid;
        else l=mid+1;
    }
    return solve(N,l,S,P,Q,X,Y);
    */
    int mn=1e9,pos;
    for (int i=0;i<M;i++){
        int need=solve1(N,i,S,P,Q,X,Y);
        if (need<1e8)
            return solve1(N,pos,S,P,Q,X,Y);
    }
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:103:9: warning: unused variable 'mn' [-Wunused-variable]
  103 |     int mn=1e9,pos;
      |         ^~
sorting.cpp:109:1: warning: control reaches end of non-void function [-Wreturn-type]
  109 | }
      | ^
sorting.cpp:107:26: warning: 'pos' is used uninitialized in this function [-Wuninitialized]
  107 |             return solve1(N,pos,S,P,Q,X,Y);
      |                    ~~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Runtime error 3 ms 544 KB Execution killed with signal 11
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 Runtime error 3 ms 544 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Runtime error 3 ms 468 KB Execution killed with signal 11
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 Runtime error 3 ms 544 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 121 ms 784 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 121 ms 784 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -