Submission #129522

# Submission time Handle Problem Language Result Execution time Memory
129522 2019-07-12T12:36:41 Z johutha Sorting (IOI15_sorting) C++14
100 / 100
186 ms 21988 KB
#include "sorting.h"
#include <vector>
#include <iostream>

using namespace std;

int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	vector<int> perm(n);
	bool bc = true;
	for (int i = 0; i < n; i++)
	{
		perm[i] = S[i];
		if (perm[i] != i) bc = false;
	}
	if (bc) return 0;
	vector<int> b = perm;
	vector<int> beg = perm;
	for (int i = 0; i < n; i++)
	{
		swap(perm[X[i]], perm[Y[i]]);
	}
	vector<int> end = perm;
	int l = 0;
	int r = n;
	while (r - l > 1)
	{
		int m = (l + r)/2;
		vector<int> mv = beg;
		for (int i = l; i < m; i++)
		{
			swap(mv[X[i]], mv[Y[i]]);
		}
		
		vector<bool> v(n, false);
		int ctr = 0;
		for (int i = 0; i < n; i++)
		{
			if (v[i]) continue;
			v[i] = true;
			int cc = 0;
			int cr = i;
			while (mv[cr] != i)
			{
				cc++;
				cr = mv[cr];
				v[cr] = true;
			}
			ctr += cc;
		}

		if (ctr <= m)
		{
			r = m;
			end = mv;
		}
		else
		{
			l = m;
			beg = mv;
		}
	}

	vector<pair<int,int>> swp;
	perm = b;

	for (int i = 0; i < n; i++)
	{
		while (end[i] != i)
		{
			swp.push_back({end[i], end[end[i]]});
			swap(end[i], end[end[i]]);
		}
	}

	vector<int> revind(n);
	for (int i = 0; i < n; i++)
	{
		revind[perm[i]] = i;
	}

	for (int i = 0; i < r; i++)
	{
		swap(revind[perm[X[i]]], revind[perm[Y[i]]]);
		swap(perm[X[i]], perm[Y[i]]);
		if (i < swp.size())
		{
			Q[i] = revind[swp[i].first];
			P[i] = revind[swp[i].second];
			swap(revind[swp[i].first], revind[swp[i].second]);
			swap(perm[Q[i]], perm[P[i]]);
		}
		else
		{
			Q[i] = 0;
			P[i] = 0;
		}
	}

	return r;
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:85:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (i < swp.size())
       ~~^~~~~~~~~~~~
sorting.cpp:7:39: warning: unused parameter 'M' [-Wunused-parameter]
 int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) {
                                       ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 4 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 4 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 3 ms 632 KB Output is correct
22 Correct 3 ms 636 KB Output is correct
23 Correct 3 ms 632 KB Output is correct
24 Correct 3 ms 504 KB Output is correct
25 Correct 3 ms 504 KB Output is correct
26 Correct 3 ms 632 KB Output is correct
27 Correct 3 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 3 ms 540 KB Output is correct
3 Correct 3 ms 432 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 3 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 508 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 4 ms 504 KB Output is correct
12 Correct 3 ms 504 KB Output is correct
13 Correct 3 ms 632 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 3 ms 540 KB Output is correct
3 Correct 3 ms 432 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 3 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 508 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 4 ms 504 KB Output is correct
12 Correct 3 ms 504 KB Output is correct
13 Correct 3 ms 632 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 158 ms 19428 KB Output is correct
16 Correct 165 ms 19888 KB Output is correct
17 Correct 181 ms 20900 KB Output is correct
18 Correct 44 ms 14200 KB Output is correct
19 Correct 135 ms 19764 KB Output is correct
20 Correct 146 ms 20456 KB Output is correct
21 Correct 147 ms 20456 KB Output is correct
22 Correct 145 ms 16200 KB Output is correct
23 Correct 169 ms 21652 KB Output is correct
24 Correct 186 ms 21240 KB Output is correct
25 Correct 182 ms 21036 KB Output is correct
26 Correct 152 ms 20324 KB Output is correct
27 Correct 147 ms 19672 KB Output is correct
28 Correct 172 ms 21232 KB Output is correct
29 Correct 172 ms 20884 KB Output is correct
30 Correct 97 ms 19568 KB Output is correct
31 Correct 170 ms 21228 KB Output is correct
32 Correct 166 ms 20872 KB Output is correct
33 Correct 178 ms 21268 KB Output is correct
34 Correct 172 ms 21348 KB Output is correct
35 Correct 137 ms 19560 KB Output is correct
36 Correct 60 ms 18296 KB Output is correct
37 Correct 184 ms 21988 KB Output is correct
38 Correct 169 ms 21124 KB Output is correct
39 Correct 176 ms 21240 KB Output is correct