제출 #133644

#제출 시각아이디문제언어결과실행 시간메모리
133644Mahdi_Jfri정렬하기 (IOI15_sorting)C++14
20 / 100
29 ms1272 KiB
#include "sorting.h"
#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back

const int maxn = 2e5 + 20;
const int maxm = 3 * maxn;

int pos[maxn] , src[maxn] , a[maxn];
int p[maxm] , q[maxm] , x[maxm] , y[maxm] , n;

bool check(int m)
{
	memcpy(a , src , sizeof src);
	if(!m)
	{
		bool f = 1;
		for(int i = 0; i < n; i++)
			f &= a[i] == i;
		return f;
	}

	swap(a[x[0]] , a[y[0]]);
	for(int i = 0; i < n; i++)
		pos[i] = i;

	for(int i = m - 1; i > 0; i--)
		swap(pos[x[i]] , pos[y[i]]);

	for(int i = 0; i < m; i++)
	{
		for(int j = 0; j < n; j++)
			if(pos[j] != a[j])
			{
				int ind = -1;
				for(int k = 0; k < n; k++)
					if(pos[k] == a[j])
					{
						while(ind >= 0);
						ind = k;
					}

				while(ind < 0);
				swap(a[ind] , a[j]);
				p[i] = ind , q[i] = j;
				break;
			}

		if(i + 1 < m)
		{
			swap(a[x[i + 1]] , a[y[i + 1]]);
			swap(pos[x[i + 1]] , pos[y[i + 1]]);
		}
	}

	bool f = 1;
	for(int i = 0; i < n; i++)
		f &= a[i] == i;
	return f;
}

int findSwapPairs(int N, int A[], int m, int X[], int Y[], int P[], int Q[])
{
	n = N;
	for(int i = 0; i < n; i++)
		src[i] = A[i];

	for(int i = 0; i < m; i++)
		x[i] = X[i] , y[i] = Y[i];

	int l = 0 , r = m;
	if(check(l))
		return l;

	while(r - l > 1)
	{
		int m = (l + r) / 2;
		if(check(m))
			r = m;
		else
			l = m;
	}

	check(r);
	for(int i = 0; i < r; i++)
		P[i] = p[i] , Q[i] = q[i];
	return r;
}




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

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:80:7: warning: declaration of 'int m' shadows a parameter [-Wshadow]
   int m = (l + r) / 2;
       ^
sorting.cpp:65:39: note: shadowed declaration is here
 int findSwapPairs(int N, int A[], int m, int X[], int Y[], int P[], int Q[])
                                       ^
#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...