Submission #860435

# Submission time Handle Problem Language Result Execution time Memory
860435 2023-10-13T02:07:42 Z pedroslrey Sorting (IOI15_sorting) C++14
0 / 100
2 ms 600 KB
#include <bits/stdc++.h>
#include "sorting.h"

using namespace std;

bool check(int n, int xs[], int k, int ss1[], int ss2[], int ans1[] = nullptr, int ans2[] = nullptr) {
	vector<int> ideal(n), idealrev(n), ys(n), ysrev(n);
	for (int i = 0; i < n; ++i)
		ideal[i] = idealrev[i] = ysrev[ys[i] = xs[i]] = i;

	for (int i = k-1; i >= 0; --i) {
		swap(ideal[ss1[i]], ideal[ss2[i]]);
		swap(idealrev[ideal[ss1[i]]], idealrev[ideal[ss2[i]]]);
	}

	int lst = 0;
	for (int i = 0; i < k; ++i) {
		// cerr << "-> " << lst << " " << idealrev[lst] << " " << ysrev[lst] << "\n";
		while (lst < n && idealrev[lst] == ysrev[lst]) ++lst;
		if (lst < n) {
			int a = ysrev[lst], b = idealrev[lst]; 
			swap(ys[a], ys[b]);
			swap(ysrev[ys[a]], ysrev[ys[b]]);

			if (ans1 != nullptr) { ans1[i] = a; ans2[i] = b; }
		}
		else if (ans1 != nullptr) { ans1[i] = 0; ans2[i] = 0; }

		swap(ideal[ss1[i]], ideal[ss2[i]]);
		swap(idealrev[ideal[ss1[i]]], idealrev[ideal[ss2[i]]]);
		swap(ys[ss1[i]], ys[ss2[i]]);
		swap(ysrev[ys[ss1[i]]], ysrev[ys[ss2[i]]]);
	}

	for (int i = 0; i < n; ++i)
		if (xs[i] != i) return false;
	return true;
}

int findSwapPairs(int n, int xs[], int m, int ss1[], int ss2[], int ans1[], int ans2[]) {
	int l = 0, r = m;
	while (l < r) {
		int m = (l + r)/2;

		if (check(n, xs, m, ss1, ss2)) r = m - 1;
		else l = m + 1;
	}

	check(n, xs, l, ss1, ss2, ans1, ans2);
	return l;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:43:7: warning: declaration of 'int m' shadows a parameter [-Wshadow]
   43 |   int m = (l + r)/2;
      |       ^
sorting.cpp:40:40: note: shadowed declaration is here
   40 | int findSwapPairs(int n, int xs[], int m, int ss1[], int ss2[], int ans1[], int ans2[]) {
      |                                    ~~~~^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 600 KB Output isn't correct
2 Halted 0 ms 0 KB -