답안 #1018460

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1018460 2024-07-10T05:34:25 Z huutuan 정렬하기 (IOI15_sorting) C++14
16 / 100
150 ms 576 KB
#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[]) {
   if (is_sorted(S, S+N)) return 0;
   for (int i=0; i<M; ++i){
      swap(S[X[i]], S[Y[i]]);
      if (is_sorted(S, S+N)) return i+1;
      vector<vector<int>> vv;
      vector<int> vis(N);
      int cnt=0;
      for (int j=0; j<N; ++j) if (!vis[j]){
         vv.emplace_back();
         int u=j;
         ++cnt;
         while (!vis[u]){
            vv.back().push_back(u); vis[u]=cnt;
            u=S[u];
         }
         if (vv.back().size()==1) vv.pop_back();
      }
      if (i+1==M){
         if ((int)vv.size()!=1 || (int)vv[0].size()!=2) return -1;
         P[i]=vv[0][0], Q[i]=vv[0][1];
         return M;
      }
      int u=X[i+1], v=Y[i+1];
      if (vis[u]==vis[v]){
         if ((int)vv[vis[u]-1].size()==2){
            int id=0;
            if (vis[u]-1==0) id=1;
            P[i]=vv[id][0], Q[i]=vv[id][1];
         }else{
            if (S[u]==v){
               P[i]=v, Q[i]=S[v];
            }else{
               P[i]=u, Q[i]=S[u];
            }
         }
      }else{
         P[i]=vv[0][0], Q[i]=vv[0][1];
      }
      swap(S[P[i]], S[Q[i]]);
      if (is_sorted(S, S+N)) return i+1;
   }
   return -1;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Runtime error 1 ms 348 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Runtime error 1 ms 348 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 348 KB Output is correct
5 Correct 1 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 Runtime error 1 ms 348 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 150 ms 576 KB Integer parameter [name=R] equals to -1, violates the range [0, 5400]
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 150 ms 576 KB Integer parameter [name=R] equals to -1, violates the range [0, 5400]
2 Halted 0 ms 0 KB -