답안 #438386

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
438386 2021-06-27T22:14:16 Z ruadhan 정렬하기 (IOI15_sorting) C++11
100 / 100
279 ms 16252 KB
#include "sorting.h"
#include <bits/stdc++.h>

using namespace std;

const int MX = 1e5 + 5;
int n, m;
vector<int> s, x, y;
vector<int> a, revA, en, revEn;

bool valid(int t)
{
	a = s;
	for (int i = 0; i < t; i++)
		swap(a[x[i]], a[y[i]]);
	for (int i = 0; i < n; i++)
		revA[a[i]] = i;
	int cnt = 0;
	for (int i = 0; i < n; i++)
		if (a[i] != i)
		{
			int j = revA[i];
			swap(revA[a[i]], revA[a[j]]);
			swap(a[i], a[j]);
			cnt++;
		}
	return cnt <= t;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
	n = N, m = M;
	s.resize(N), x.resize(N), y.resize(N), a.resize(N), revA.resize(N), en.resize(N), revEn.resize(N);

	for (int i = 0; i < N; i++)
	{
		s.at(i) = S[i];
		x.at(i) = X[i];
		y.at(i) = Y[i];
	}
	int R = 0;
	for (int b = N / 2; b >= 1; b /= 2)
	{
		while (R + b <= M && !valid(R + b))
			R += b;
	}
	if (R == 0)
		R += (valid(R) ? 0 : 1);
	else
		R++;

	a = s;
	for (int i = 0; i < n; i++)
		revA[a[i]] = i;
	en = a;
	for (int i = 0; i < R; i++)
		swap(en[x[i]], en[y[i]]);
	for (int i = 0; i < n; i++)
		revEn[en[i]] = i;

	queue<pair<int, int>> changes;
	vector<int> tmp = en;

	for (int i = 0; i < n; i++)
		if (tmp[i] != i)
		{
			int j = revEn[i];
			swap(revEn[tmp[i]], revEn[tmp[j]]);
			swap(tmp[i], tmp[j]);
			changes.push({i, j});
		}
	for (int i = 0; i < R; i++)
	{
		swap(revA[a[x[i]]], revA[a[y[i]]]);
		swap(a[x[i]], a[y[i]]);
		if (!changes.empty())
		{
			auto change = changes.front();
			changes.pop();
			P[i] = revA[en[change.first]];
			Q[i] = revA[en[change.second]];
			swap(en[change.first], en[change.second]);
			swap(revA[a[P[i]]], revA[a[Q[i]]]);
			swap(a[P[i]], a[Q[i]]);
		}
		else
			P[i] = Q[i] = 1;
	}

	return R;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 304 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 304 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 344 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 344 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 235 ms 13824 KB Output is correct
16 Correct 265 ms 14060 KB Output is correct
17 Correct 261 ms 14816 KB Output is correct
18 Correct 72 ms 11076 KB Output is correct
19 Correct 178 ms 13528 KB Output is correct
20 Correct 186 ms 14088 KB Output is correct
21 Correct 196 ms 14148 KB Output is correct
22 Correct 230 ms 15132 KB Output is correct
23 Correct 263 ms 15344 KB Output is correct
24 Correct 273 ms 15132 KB Output is correct
25 Correct 272 ms 14900 KB Output is correct
26 Correct 170 ms 14132 KB Output is correct
27 Correct 173 ms 13508 KB Output is correct
28 Correct 250 ms 16252 KB Output is correct
29 Correct 229 ms 15640 KB Output is correct
30 Correct 118 ms 12656 KB Output is correct
31 Correct 230 ms 15980 KB Output is correct
32 Correct 269 ms 14916 KB Output is correct
33 Correct 279 ms 15264 KB Output is correct
34 Correct 259 ms 15148 KB Output is correct
35 Correct 185 ms 13480 KB Output is correct
36 Correct 66 ms 11844 KB Output is correct
37 Correct 261 ms 15476 KB Output is correct
38 Correct 222 ms 14900 KB Output is correct
39 Correct 272 ms 15016 KB Output is correct