제출 #155092

#제출 시각아이디문제언어결과실행 시간메모리
155092dennisstar정렬하기 (IOI15_sorting)C++11
100 / 100
756 ms25052 KiB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int *s, *x, *y, *p, *q;
int ans;
int n, m;
int A[200010], B[200010], posB[200010], F[200010], posF[200010];
//현재 에르맥, 현재 수열, 현재 수열의 pos, 결과 수열, 결과 수열의 pos
int seq[600010][2], tp;
bool canSwap(int l)
{
	int i; tp=0;
	for (i=0; i<n; i++) A[i]=i, B[i]=s[i], posB[s[i]]=i, F[i]=s[i], posF[s[i]]=i;
	for (i=0; i<l; i++) {
		swap(F[x[i]], F[y[i]]);
		swap(posF[F[x[i]]], posF[F[y[i]]]);
	}
	for (i=l-1; i>=0; i--) swap(A[x[i]], A[y[i]]);
	while (tp<n&&F[tp]==tp) tp++;
	for (i=0; i<l; i++) {
		swap(A[x[i]], A[y[i]]);
		swap(B[x[i]], B[y[i]]);
		swap(posB[B[x[i]]], posB[B[y[i]]]);
		int i1=F[tp], i2=F[posF[tp]];
		swap(F[tp], F[posF[tp]]);
		swap(posF[i1], posF[i2]);
		seq[i][0]=posB[i1], seq[i][1]=posB[i2];
		swap(B[posB[i1]], B[posB[i2]]);
		swap(posB[i1], posB[i2]);
		//for (int j=0; j<n; j++) printf("%d ", A[j]); puts("");
		//for (int j=0; j<n; j++) printf("%d ", B[j]); puts("");
		//for (int j=0; j<n; j++) printf("%d ", F[j]); puts("");
		//puts("");
		while (tp<n&&F[tp]==tp) tp++;
		if (tp>=n) break;
	}
	if (tp>=n) {
		//printf("%d\n", l);
		for (int j=0; j<=i; j++) p[j]=seq[j][0], q[j]=seq[j][1];
		for (int j=i+1; j<l; j++) p[j]=q[j]=1;
		ans=l;
		return true;
	}
	return false;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    s=S; x=X; y=Y; p=P; q=Q; n=N; m=M;
	int st=0, re=M;
	while (st<re) {
		int md=(st+re)/2;
		if (canSwap(md)) re=md;
		else st=md+1;
		//puts("");
	}
	return ans;
}
#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...