Submission #292347

# Submission time Handle Problem Language Result Execution time Memory
292347 2020-09-06T20:56:07 Z VodkaInTheJar Sorting (IOI15_sorting) C++14
100 / 100
191 ms 20444 KB
#include <bits/stdc++.h>
 
using namespace std;

const int maxn = 2e5 + 3; 

int n; 
int t[maxn];
int pos[maxn];
bool used[maxn];

int get_cycles()
{
	for (int i = 0; i < n; i++)
	used[i] = false; 
	
	int ans = 0; 
	for (int i = 0; i < n; i++)
	if (!used[i])
	{
		ans++;
		int j = i;
		while (!used[j])
		{
			used[j] = true;
			j = t[j];
		}
	}
	
	return ans; 
}

int findSwapPairs(int _n, int s[], int m, int x[], int y[], int p[], int q[])
{
	n = _n; 
	for (int i = 0; i < n; i++)
	pos[s[i]] = i;
	
	for (int i = 0; i < n; i++)
	t[i] = s[i];
	
	if (get_cycles() == n)
	return 0; 
	
	int low = 0, high = m-1;
	while (low < high)
	{
		int mid = (low + high) / 2; 
		for (int i = 0; i < n; i++)
		t[i] = s[i];
		
		for (int i = 0; i <= mid; i++)
		swap(t[x[i]], t[y[i]]);
		
		if (n - get_cycles() <= mid + 1)
		high = mid;
		
		else 
		low = mid + 1; 
	}
	
	for (int i = 0; i < n; i++)
		t[i] = s[i];
	
	for (int i = 0; i < m; i++)
	{
		swap(t[x[i]], t[y[i]]);
		
		if (i == low)
		{
			vector <pair <int, int> > v; 
			for (int j = 0; j < n; j++)
			used[j] = false; 
			
			for (int j = 0; j < n; j++)
			if (!used[j])
			{
				int curr = j;
				int last = j; 
				while (!used[curr])
				{
					used[curr] = true;
					if (curr != last)
					v.push_back({last, curr});
					
					last = curr;
					curr = t[curr];
				}
			}
			
			for (int j = 0; j <= i; j++)
			{
				if (x[j] != y[j])
				{
					swap(pos[s[x[j]]], pos[s[y[j]]]);
					swap(s[x[j]], s[y[j]]);
				}
				
				if (j < (int)v.size())
				{
					p[j] = pos[v[j].first];
					q[j] = pos[v[j].second];
					swap(pos[s[p[j]]], pos[s[q[j]]]);
					swap(s[p[j]], s[q[j]]);
				}
				
				else 
				p[j] = q[j] = 0; 
			}
			
			return i + 1; 
		}
	}
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:114:1: warning: control reaches end of non-void function [-Wreturn-type]
  114 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 288 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 288 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 288 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 2 ms 640 KB Output is correct
24 Correct 1 ms 512 KB Output is correct
25 Correct 1 ms 516 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 516 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 516 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 160 ms 18136 KB Output is correct
16 Correct 171 ms 18780 KB Output is correct
17 Correct 186 ms 19616 KB Output is correct
18 Correct 47 ms 15100 KB Output is correct
19 Correct 152 ms 17960 KB Output is correct
20 Correct 155 ms 18516 KB Output is correct
21 Correct 162 ms 18604 KB Output is correct
22 Correct 155 ms 14968 KB Output is correct
23 Correct 178 ms 20444 KB Output is correct
24 Correct 190 ms 20008 KB Output is correct
25 Correct 186 ms 19844 KB Output is correct
26 Correct 161 ms 18652 KB Output is correct
27 Correct 143 ms 18060 KB Output is correct
28 Correct 187 ms 20024 KB Output is correct
29 Correct 180 ms 19536 KB Output is correct
30 Correct 112 ms 17388 KB Output is correct
31 Correct 186 ms 19768 KB Output is correct
32 Correct 177 ms 19596 KB Output is correct
33 Correct 188 ms 20084 KB Output is correct
34 Correct 175 ms 19960 KB Output is correct
35 Correct 149 ms 17920 KB Output is correct
36 Correct 61 ms 16120 KB Output is correct
37 Correct 191 ms 20164 KB Output is correct
38 Correct 180 ms 19844 KB Output is correct
39 Correct 182 ms 19192 KB Output is correct