Submission #785328

# Submission time Handle Problem Language Result Execution time Memory
785328 2023-07-17T08:23:47 Z NothingXD Sorting (IOI15_sorting) C++17
100 / 100
158 ms 17076 KB
#include "sorting.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef complex<ld> point;

void debug_out(){cerr << endl;}

template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T){
	cout << H << " ";
	debug_out(T...);
}

#define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", debug_out(__VA_ARGS__)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)

const int maxn = 2e5 + 10;

int n, a[maxn], b[maxn], idx[maxn], x[3*maxn], y[3*maxn];
bool vis[maxn];

bool check(int k){
	for (int i = 0; i < n; i++) b[i] = a[i];
	for (int i = 0; i < k; i++){
		swap(b[x[i]], b[y[i]]);
	}
	memset(vis, false, sizeof vis);
	int cnt = 0;
	for (int i = 0; i < n; i++){
		if (!vis[i]) cnt++;
		int x = i;
		while(!vis[x]){
			vis[x] = true;
			x = b[x];
		}
	}
	return n - cnt <= k;
}

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++){
		a[i] = S[i];
		idx[a[i]] = i;
	}
	for (int i = 0; i < M; i++){
		x[i] = X[i];
		y[i] = Y[i];
	}
	int l = -1, r = M;
	while(l + 1 < r){
		int mid = (l+r) >> 1;
		if (check(mid)) r = mid;
		else l = mid;
	}

	for (int i = 0; i < n; i++){
		b[i] = a[i];
	}
	for (int i = 0; i < r; i++){
		swap(b[x[i]], b[y[i]]);
	}

	vector<pii> swp;
	memset(vis, false, sizeof vis);
	for (int i = 0; i < n; i++){
		int x = i;
		vector<int> ver;
		while(!vis[x]){
			ver.push_back(x);
			vis[x] = true;
			x = b[x];
		}
		for (int j = 1; j < ver.size(); j++){
			swp.push_back(MP(ver[j-1], ver[j]));
		}
	}

	for (int i = 0; i < r; i++){
		swap(a[x[i]], a[y[i]]);
		swap(idx[a[x[i]]], idx[a[y[i]]]);
		if (i < swp.size()){
			P[i] = idx[swp[i].F];
			Q[i] = idx[swp[i].S];
		}
		else{
			P[i] = Q[i] = 0;
		}
		swap(a[P[i]], a[Q[i]]);
		swap(idx[a[P[i]]], idx[a[Q[i]]]);
	}

	return r;
}

Compilation message

sorting.cpp: In function 'bool check(int)':
sorting.cpp:40:7: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   40 |   int x = i;
      |       ^
sorting.cpp:28:37: note: shadowed declaration is here
   28 | int n, a[maxn], b[maxn], idx[maxn], x[3*maxn], y[3*maxn];
      |                                     ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:77:7: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   77 |   int x = i;
      |       ^
sorting.cpp:28:37: note: shadowed declaration is here
   28 | int n, a[maxn], b[maxn], idx[maxn], x[3*maxn], y[3*maxn];
      |                                     ^
sorting.cpp:84:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |   for (int j = 1; j < ver.size(); j++){
      |                   ~~^~~~~~~~~~~~
sorting.cpp:92:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |   if (i < swp.size()){
      |       ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 0 ms 468 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 0 ms 468 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 0 ms 468 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 0 ms 468 KB Output is correct
20 Correct 0 ms 468 KB Output is correct
21 Correct 2 ms 724 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 1 ms 724 KB Output is correct
27 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 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 596 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 112 ms 15648 KB Output is correct
16 Correct 118 ms 15788 KB Output is correct
17 Correct 134 ms 16740 KB Output is correct
18 Correct 56 ms 12044 KB Output is correct
19 Correct 108 ms 14616 KB Output is correct
20 Correct 117 ms 15156 KB Output is correct
21 Correct 115 ms 15152 KB Output is correct
22 Correct 107 ms 17060 KB Output is correct
23 Correct 134 ms 17032 KB Output is correct
24 Correct 132 ms 17076 KB Output is correct
25 Correct 135 ms 16920 KB Output is correct
26 Correct 115 ms 15208 KB Output is correct
27 Correct 111 ms 14680 KB Output is correct
28 Correct 132 ms 16696 KB Output is correct
29 Correct 129 ms 16172 KB Output is correct
30 Correct 83 ms 14012 KB Output is correct
31 Correct 127 ms 16436 KB Output is correct
32 Correct 123 ms 16708 KB Output is correct
33 Correct 158 ms 17052 KB Output is correct
34 Correct 129 ms 16720 KB Output is correct
35 Correct 107 ms 14568 KB Output is correct
36 Correct 49 ms 12940 KB Output is correct
37 Correct 134 ms 16604 KB Output is correct
38 Correct 127 ms 16512 KB Output is correct
39 Correct 128 ms 15968 KB Output is correct