# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
140468 | PedroBigMan | 정렬하기 (IOI15_sorting) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "sorting.h"
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
typedef int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=a; i<b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define INF ((ll) pow(2,63) -1)
ll insig;
#define In(vecBRO, LENBRO) REP(IBRO,0,LENBRO) {cin>>insig; vecBRO.pb(insig);}
void Out(vector<ll> x) {REP(i,0,x.size()) {cout<<x[i]<<" ";} cout<<endl;}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
ll c1, c2;
vector<ll> pos, oc;
REP(i,0,N) {pos.pb(i);oc.pb(0);}
REP(i,0,N) {oc[S[i]]=i;}
ll last=0;
REP(i,0,M)
{
c1=pos[X[i]]; c2=pos[Y[i]];
pos[X[i]]=c2; pos[Y[i]]=c1;
}
vector<ll> m; REP(i,0,N) {m.pb(0);}
REP(i,0,N) {m[pos[i]]=i;}
ll sP=0;
REP(i,0,M)
{
bool srt = true; REP(i,0,N-1) {if(S[i]>S[i+1]) {srt=false;}}
if(srt) {return sP;}
//swap cells X[i],Y[i] because of E
oc[S[X[i]]]=Y[i]; oc[S[Y[i]]]=X[i];
c1=S[X[i]]; c2=S[Y[i]]; S[X[i]]=c2; S[Y[i]]=c1;
c1=pos[m[X[i]]]; c2=pos[m[Y[i]]]; pos[m[X[i]]]=c2; pos[m[Y[i]]]=c1;
c1=m[X[i]]; c2=m[Y[i]]; m[X[i]]=c2; m[Y[i]]=c1;
//if i<N swap cells oc[i],pos[i] because of A
if(i<N)
{
P[i] = oc[i]; Q[i] = pos[i];
c1=S[oc[i]]; c2=S[pos[i]];
oc[S[pos[i]]] = oc[i]; oc[i] = pos[i];
S[P[i]]=c2; S[Q[i]]=c1;
}
else {P[sP]=0; Q[sP]=0;}
sP++;
}
return sP;
}
int main()
{
ll N,M; cin>>N>>M; int S[N]; REP(i,0,N) {cin>>S[i];}
int X[M],Y[M]; REP(i,0,M) {cin>>X[i]>>Y[i];}
int P[M], Q[M];
cout<<findSwapPairs(N,S,M,X,Y,P,Q)<<endl;
}