Submission #785462

# Submission time Handle Problem Language Result Execution time Memory
785462 2023-07-17T09:20:52 Z Sohsoh84 Sorting (IOI15_sorting) C++17
74 / 100
1000 ms 22416 KB
#include "sorting.h"
#include <bits/stdc++.h>

using namespace std;

#define debug(x)		cerr << #x << ": " << x << endl;
#define sep			' '

const int MAXN = 1e6 + 10;

int X[MAXN], Y[MAXN], n, m, ans_A[MAXN], ans_B[MAXN], T[MAXN], P[MAXN], S[MAXN], ind[MAXN];

inline void check(set<int>& st, int x) {
	if (st.find(x) != st.end()) st.erase(x);
	if (P[x] != x) st.insert(x);
}

inline bool solve(int m) {
	// P: jaye mellat too jaygasht nahayii baad swap ha
	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]]);
	for (int i = 0; i < n; i++) P[T[i]] = i;

	for (int i = 0; i < n; i++) { 
		T[i] = S[i];
		ind[S[i]] = i;
	}

	set<int> st;
	for (int i = 0; i < n; i++)
		check(st, i);

	for (int i = 0; i <= m; i++) {
		swap(T[X[i]], T[Y[i]]);
		ind[T[X[i]]] = X[i];
		ind[T[Y[i]]] = Y[i];
		
		if (st.empty()) ans_A[i] = ans_B[i] = 0;
		else {
			int e = *st.begin();

			ans_A[i] = ind[e];
			ans_B[i] = ind[P[e]];

			int a = e, b = P[e];
			swap(P[e], P[P[e]]);
			check(st, a);
			check(st, b);
		}

		swap(T[ans_A[i]], T[ans_B[i]]);
		ind[T[ans_A[i]]] = ans_A[i];
		ind[T[ans_B[i]]] = ans_B[i];
	}

	return st.empty();
}

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++)
		S[i] = S_[i];

	for (int i = 0; i < m; i++)
		X[i] = X_[i], Y[i] = Y_[i];

	bool flag = true;
	for (int i = 0; i < n; i++)
		if (S[i] != i)
			flag = false;
	
	if (flag) return 0;

	int l = 0, r = m - 1;
	while (l < r) {
		int mid = (l + r) >> 1;
		if (solve(mid)) r = mid;
		else l = mid + 1;
	}

	solve(l);
	
	for (int i = 0; i <= l; i++)
		P[i] = ans_A[i], Q[i] = ans_B[i];
	return l + 1;
}


Compilation message

sorting.cpp: In function 'bool solve(int)':
sorting.cpp:18:23: warning: declaration of 'm' shadows a global declaration [-Wshadow]
   18 | inline bool solve(int m) {
      |                   ~~~~^
sorting.cpp:11:26: note: shadowed declaration is here
   11 | int X[MAXN], Y[MAXN], n, m, ans_A[MAXN], ans_B[MAXN], T[MAXN], P[MAXN], S[MAXN], ind[MAXN];
      |                          ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:59:69: warning: declaration of 'P' shadows a global declaration [-Wshadow]
   59 | int findSwapPairs(int N_, int S_[], int M_, int X_[], int Y_[], int P[], int Q[]) {
      |                                                                 ~~~~^~~
sorting.cpp:11:64: note: shadowed declaration is here
   11 | int X[MAXN], Y[MAXN], n, m, ans_A[MAXN], ans_B[MAXN], T[MAXN], P[MAXN], S[MAXN], ind[MAXN];
      |                                                                ^
# 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 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 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 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 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 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 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 0 ms 340 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 3 ms 596 KB Output is correct
22 Correct 4 ms 596 KB Output is correct
23 Correct 3 ms 596 KB Output is correct
24 Correct 3 ms 596 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 3 ms 596 KB Output is correct
27 Correct 3 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 616 KB Output is correct
2 Correct 8 ms 616 KB Output is correct
3 Correct 8 ms 600 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 6 ms 596 KB Output is correct
8 Correct 11 ms 600 KB Output is correct
9 Correct 9 ms 596 KB Output is correct
10 Correct 9 ms 612 KB Output is correct
11 Correct 9 ms 596 KB Output is correct
12 Correct 5 ms 596 KB Output is correct
13 Correct 9 ms 596 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 616 KB Output is correct
2 Correct 8 ms 616 KB Output is correct
3 Correct 8 ms 600 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 3 ms 596 KB Output is correct
6 Correct 4 ms 596 KB Output is correct
7 Correct 6 ms 596 KB Output is correct
8 Correct 11 ms 600 KB Output is correct
9 Correct 9 ms 596 KB Output is correct
10 Correct 9 ms 612 KB Output is correct
11 Correct 9 ms 596 KB Output is correct
12 Correct 5 ms 596 KB Output is correct
13 Correct 9 ms 596 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Execution timed out 1080 ms 22416 KB Time limit exceeded
16 Halted 0 ms 0 KB -