Submission #438367

# Submission time Handle Problem Language Result Execution time Memory
438367 2021-06-27T21:43:11 Z ruadhan Sorting (IOI15_sorting) C++11
62 / 100
303 ms 17864 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 + 1) ? 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;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 1 ms 204 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 1 ms 204 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 1 ms 204 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 1 ms 432 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 460 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 460 KB Output is correct
13 Correct 3 ms 460 KB Output is correct
14 Correct 2 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 1 ms 432 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 460 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 460 KB Output is correct
13 Correct 3 ms 460 KB Output is correct
14 Correct 2 ms 460 KB Output is correct
15 Correct 229 ms 15492 KB Output is correct
16 Correct 244 ms 15688 KB Output is correct
17 Correct 268 ms 16428 KB Output is correct
18 Correct 78 ms 12652 KB Output is correct
19 Correct 191 ms 15304 KB Output is correct
20 Correct 187 ms 15784 KB Output is correct
21 Correct 197 ms 15860 KB Output is correct
22 Correct 235 ms 16680 KB Output is correct
23 Correct 291 ms 17044 KB Output is correct
24 Correct 303 ms 16800 KB Output is correct
25 Correct 271 ms 16572 KB Output is correct
26 Correct 175 ms 15768 KB Output is correct
27 Correct 171 ms 15172 KB Output is correct
28 Correct 255 ms 17864 KB Output is correct
29 Correct 234 ms 17364 KB Output is correct
30 Correct 121 ms 14268 KB Output is correct
31 Correct 239 ms 17768 KB Output is correct
32 Correct 275 ms 16620 KB Output is correct
33 Correct 279 ms 16872 KB Output is correct
34 Correct 261 ms 16704 KB Output is correct
35 Correct 198 ms 15172 KB Output is correct
36 Correct 76 ms 13548 KB Output is correct
37 Correct 269 ms 17132 KB Output is correct
38 Correct 238 ms 16516 KB Output is correct
39 Correct 280 ms 16696 KB Output is correct