Submission #532752

# Submission time Handle Problem Language Result Execution time Memory
532752 2022-03-03T20:16:17 Z sliviu Sorting (IOI15_sorting) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>

using namespace std;

int findSwapPairs(int n, int s[], int m, int x[], int y[], int s1[], int s2[]) {
	vector<int> p(n);
	for (int i = 0; i < n; ++i)
		p[i] = s[i];
	int ok = 1;
	for (int i = 0; i < n; ++i)
		if (p[i] != i)
			ok = 0;
	if (ok)
		return 0;
	int left = 0, right = n - 1;
	auto ok = [&](int pos) {
		auto p_ = p;
		for (int i = 0; i <= pos; ++i)
			swap(p_[x[i]], p_[y[i]]);
		int ans = n;
		vector<int> seen(n);
		for (int i = 0; i < n; ++i)
			if (!seen[i]) {
				--ans;
				seen[i] = 1;
				for (int j = p_[i]; j != i; j = p_[j])
					seen[j] = 1;
			}
		return ans <= pos + 1;
	};
	while (left < right) {
		int m = (left + right) / 2;
		if (ok(m))
			right = m;
		else
			left = m + 1;
	}
	auto p_ = p;
	for (int i = 0; i <= left; ++i)
		swap(p_[x[i]], p_[y[i]]);
	int cur = 0;
	vector<int> seen(n), inv(n);
	for (int i = 0; i < n; ++i)
		if (!seen[i] && p_[i] != i) {
			vector<int> sol = {i};
			for (int j = p_[i]; j != i; j = p_[j])
				sol.emplace_back(j), seen[j] = 1;
			for (int i = 0; i < (int)sol.size() - 1; ++i)
				s1[cur] = sol[i], s2[cur++] = sol[i + 1];
		}
	for (int i = 0; i < n; ++i)
		inv[p[i]] = i;
	for (int i = 0; i <= left; ++i) {
		swap(inv[p[x[i]]], inv[p[y[i]]]);
		swap(p[x[i]], p[y[i]]);
		s1[i] = inv[s1[i]], s2[i] = inv[s2[i]];
		swap(inv[p[s1[i]]], inv[p[s2[i]]]);
		swap(p[s1[i]], p[s2[i]]);
	}
	return left + 1;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:16:7: error: conflicting declaration 'auto ok'
   16 |  auto ok = [&](int pos) {
      |       ^~
sorting.cpp:9:6: note: previous declaration as 'int ok'
    9 |  int ok = 1;
      |      ^~
sorting.cpp:32:7: warning: declaration of 'int m' shadows a parameter [-Wshadow]
   32 |   int m = (left + right) / 2;
      |       ^
sorting.cpp:5:39: note: shadowed declaration is here
    5 | int findSwapPairs(int n, int s[], int m, int x[], int y[], int s1[], int s2[]) {
      |                                   ~~~~^
sorting.cpp:33:11: error: 'ok' cannot be used as a function
   33 |   if (ok(m))
      |           ^
sorting.cpp:48:13: warning: declaration of 'i' shadows a previous local [-Wshadow]
   48 |    for (int i = 0; i < (int)sol.size() - 1; ++i)
      |             ^
sorting.cpp:43:11: note: shadowed declaration is here
   43 |  for (int i = 0; i < n; ++i)
      |           ^
sorting.cpp:5:39: warning: unused parameter 'm' [-Wunused-parameter]
    5 | int findSwapPairs(int n, int s[], int m, int x[], int y[], int s1[], int s2[]) {
      |                                   ~~~~^