제출 #138012

#제출 시각아이디문제언어결과실행 시간메모리
138012arthurconmy정렬하기 (IOI15_sorting)C++14
0 / 100
6 ms632 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 500;

int eend[MAXN];
int eend_inv[MAXN];

void eend_swap(int a, int b)
{
  int one = eend[a];
  int two = eend[b];

  eend[a]=two;
  eend[b]=one;
}

void eend_inv_swap(int a, int b)
{
  int one = eend_inv[a];
  int two = eend_inv[b];

  eend_inv[a]=two;
  eend_inv[b]=one;
}

int findSwapPairs(int n, int S[MAXN], int m, int X[MAXN], int Y[MAXN], int P[MAXN], int Q[MAXN])
{
  // cout << n << endl;

  for(int i=0; i<n; i++)
  {
    eend[i]=S[i];
  }

  for(int i=0; i<m; i++)
  {
    eend_swap(X[i],Y[i]);
  }

  for(int i=0; i<n; i++)
  {
    eend_inv[i]=eend[i];
  }

  // for(int i=0; i<n; i++)
  // {
  //   cout << i << " " << eend[i] << endl;
  // }

  int cur_sort=0;

  for(int i=0; i<m; i++)
  {
    eend_inv_swap(X[i],Y[i]); // correct ???

    int one = S[X[i]];
    int two = S[Y[i]];

    S[X[i]]=two;
    S[Y[i]]=one;

    bool upd=0;

    while(cur_sort < n)
    {
      int pos_s=-1;
      int pos_e=-1;

      for(int j=0; j<n; j++)
      {
        if(S[j]==cur_sort) pos_s=j;
        if(eend_inv[j]==cur_sort) pos_e=j;
      }

      if(pos_e == pos_s && pos_e != -1)
      {
        // cout << cur_sort << " is sorted bro" << endl;

        cur_sort++;
        continue;
      }

      one = S[pos_s];
      two = S[pos_e];

      S[pos_s]=two;
      S[pos_e]=one;

      P[i]=pos_s;
      Q[i]=pos_e;
      upd=1;

      cur_sort++;
      break;
    }  
    
    if(!upd)
    {
      P[i]=0;
      Q[i]=0;
    }

    //cout << P[i] << " " << Q[i] << endl;
  }

  //cout << "done" << endl;

  return m;
}
#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...