제출 #1096509

#제출 시각아이디문제언어결과실행 시간메모리
1096509onlk97정렬하기 (IOI15_sorting)C++14
36 / 100
17 ms604 KiB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]){
    int r;
    int a[N];
    for (int i=0; i<N; i++) a[i]=S[i];
    for (int i=0; i<M; i++){
        swap(a[X[i]],a[Y[i]]);
        bool vis[N];
        for (int j=0; j<N; j++) vis[j]=0;
        int cnt=0;
        for (int j=0; j<N; j++){
            if (vis[j]) continue;
            cnt++;
            int tp=j;
            while (!vis[tp]){
                vis[tp]=1;
                tp=a[tp];
            }
        }
        if (N-cnt<=i+1){
            r=i+1;
            break;
        }
    }
    bool allsame=1;
    for (int i=0; i<N; i++) if (S[i]!=i) allsame=0;
    if (allsame) r=0;
    for (int i=0; i<r; i++){
        swap(S[X[i]],S[Y[i]]);
        bool stillhave[N];
        for (int j=0; j<N; j++) stillhave[j]=0;
        for (int j=i+1; j<r; j++){
            if (X[j]==Y[j]) continue;
            stillhave[X[j]]=1; stillhave[Y[j]]=1;
        }
        int haha=-1;
        for (int j=0; j<N; j++){
            if (!stillhave[j]&&S[j]!=j) haha=j;
        }
        if (haha==-1){
            P[i]=0; Q[i]=0;
            continue;
        }
        for (int j=0; j<N; j++) if (S[j]==haha) P[i]=j;
        Q[i]=haha;
        swap(S[P[i]],S[Q[i]]);
    }
    return r;
}

컴파일 시 표준 에러 (stderr) 메시지

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:34:26: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
   34 |         for (int j=i+1; j<r; j++){
      |                         ~^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...