Submission #788060

# Submission time Handle Problem Language Result Execution time Memory
788060 2023-07-19T17:35:54 Z GusterGoose27 Sorting (IOI15_sorting) C++17
100 / 100
174 ms 29748 KB
#include "sorting.h"

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

const int MAXN = 2e5+5;

int perm[MAXN];
int swap_perm[MAXN]; // the thing at position 1 at the end is currently at position ...
int inv_swap[MAXN]; // the thing at position 1 now will be at position ... at the end
int swap_final[MAXN];
int inv_final[MAXN];
vector<pii> pairs;
int n, m;
vector<pii> swaps_mx;
vector<pii> swaps;

void make_swaps() {
	swaps.clear();
	for (int i = 0; i < n; i++) inv_final[swap_final[i]] = i;
	for (int i = 0; i < n; i++) {
		if (inv_final[i] == i) continue;
		int pos = inv_final[i];
		swaps.push_back(pii(i, pos));
		swap(inv_final[swap_final[i]], inv_final[i]);
		swap(swap_final[i], swap_final[pos]);
	}
}

bool works(int cur) {
	iota(swap_perm, swap_perm+n, 0);
	for (int i = 0; i < cur; i++) swap(swap_perm[pairs[i].first], swap_perm[pairs[i].second]);
	for (int i = 0; i < n; i++) inv_swap[swap_perm[i]] = i;
	for (int i = 0; i < n; i++) swap_final[i] = perm[swap_perm[i]];
	make_swaps();
	if (swaps.size() > cur) return 0;
	while (swaps.size() < cur) swaps.push_back(pii(0, 0));
	for (int i = 0; i < swaps.size(); i++) {
		swap(swap_perm[inv_swap[pairs[i].first]], swap_perm[inv_swap[pairs[i].second]]);
		swap(inv_swap[pairs[i].first], inv_swap[pairs[i].second]);
		swaps[i].first = swap_perm[swaps[i].first];
		swaps[i].second = swap_perm[swaps[i].second];
	}
	swaps_mx.clear();
	for (pii p: swaps) swaps_mx.push_back(p);
	return 1;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	n = N;
	m = M;
	for (int i = 0; i < n; i++) perm[i] = S[i];
	for (int i = 0; i < m; i++) pairs.push_back(pii(X[i], Y[i]));
	int mn = -1, mx = M+1;
	works(3);
	while (mx > mn+1) {
		int cur = (mn+mx)/2;
		if (works(cur)) mx = cur;
		else mn = cur;
	}
	for (int i = 0; i < mx; i++) {
		P[i] = swaps_mx[i].first;
		Q[i] = swaps_mx[i].second;
	}
	return mx;
}


Compilation message

sorting.cpp: In function 'bool works(int)':
sorting.cpp:39:19: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   39 |  if (swaps.size() > cur) return 0;
      |      ~~~~~~~~~~~~~^~~~~
sorting.cpp:40:22: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   40 |  while (swaps.size() < cur) swaps.push_back(pii(0, 0));
      |         ~~~~~~~~~~~~~^~~~~
sorting.cpp:41:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |  for (int i = 0; i < swaps.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 444 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 444 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 312 KB Output is correct
14 Correct 1 ms 308 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 852 KB Output is correct
22 Correct 1 ms 852 KB Output is correct
23 Correct 2 ms 824 KB Output is correct
24 Correct 2 ms 852 KB Output is correct
25 Correct 2 ms 852 KB Output is correct
26 Correct 2 ms 824 KB Output is correct
27 Correct 2 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 584 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 636 KB Output is correct
4 Correct 1 ms 580 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 2 ms 580 KB Output is correct
11 Correct 2 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 2 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 584 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 636 KB Output is correct
4 Correct 1 ms 580 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 2 ms 580 KB Output is correct
11 Correct 2 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 2 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 135 ms 27080 KB Output is correct
16 Correct 139 ms 27464 KB Output is correct
17 Correct 157 ms 28664 KB Output is correct
18 Correct 75 ms 24116 KB Output is correct
19 Correct 119 ms 27188 KB Output is correct
20 Correct 119 ms 27764 KB Output is correct
21 Correct 118 ms 27836 KB Output is correct
22 Correct 122 ms 28560 KB Output is correct
23 Correct 152 ms 29464 KB Output is correct
24 Correct 174 ms 29096 KB Output is correct
25 Correct 159 ms 28848 KB Output is correct
26 Correct 127 ms 27716 KB Output is correct
27 Correct 119 ms 27076 KB Output is correct
28 Correct 144 ms 28964 KB Output is correct
29 Correct 143 ms 28456 KB Output is correct
30 Correct 107 ms 26356 KB Output is correct
31 Correct 152 ms 28904 KB Output is correct
32 Correct 159 ms 28772 KB Output is correct
33 Correct 161 ms 29152 KB Output is correct
34 Correct 144 ms 29028 KB Output is correct
35 Correct 117 ms 26744 KB Output is correct
36 Correct 66 ms 25428 KB Output is correct
37 Correct 167 ms 29748 KB Output is correct
38 Correct 150 ms 28772 KB Output is correct
39 Correct 159 ms 28968 KB Output is correct