Submission #387242

# Submission time Handle Problem Language Result Execution time Memory
387242 2021-04-08T07:16:52 Z Keshi Sorting (IOI15_sorting) C++17
100 / 100
206 ms 30104 KB
//In the name of God
#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;

typedef int ll;
typedef pair<ll, ll> pll;

const ll maxn = 2e5 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e9;

#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r+", stdin);freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()

int g[maxn], a[maxn], b[maxn];
bool vis[maxn];
int n, x[maxn], y[maxn], s[maxn];

bool ok(ll j){
	for(ll i = 0; i < n; i++){
		a[i] = s[i];
	}
	for(ll i = 0; i < j; i++){
		swap(a[x[i]], a[y[i]]);
	}
		for(ll i = 0; i < n; i++){
			g[a[i]] = i;
			vis[i] = 0;
		}
		ll cnt = 0;
		for(ll i = 0; i < n; i++){
			if(vis[i]) continue;
			cnt++;
			ll v = i;
			do{
				vis[v] = 1;
				v = g[v];
			}while(v != i);
		}
	return (j >= n - cnt);
}

int findSwapPairs(int N, int S[], int m, int X[], int Y[], int p[], int q[]) {
	n = N;
	for(ll i = 0; i < n; i++){
		s[i] = S[i];
		x[i] = X[i];
		y[i] = Y[i];
	}
	ll l = -1, r = n, mid;
	while(r - l > 1){
		mid = (l + r) >> 1;
		if(ok(mid)) r = mid;
		else l = mid;
	}
	ll j = r;
	for(ll i = 0; i < n; i++){
		a[i] = s[i];
	}
	for(ll i = 0; i < j; i++){
		swap(a[x[i]], a[y[i]]);
	}
		for(ll i = 0; i < n; i++){
			g[a[i]] = i;
			vis[i] = 0;
		}
		vector<vector<ll> > vec;
		for(ll i = 0; i < n; i++){
			if(vis[i]) continue;
			vector<ll> v2;
			ll v = i;
			do{
				vis[v] = 1;
				v2.pb(v);
				v = g[v];
			}while(v != i);
			vec.pb(v2);
		}
			vector<ll> v2(10);
			vec.pb(v2);
			ll p1 = 0;
			ll p2 = 1;
			for(ll i = 0; i < n; i++){
				b[s[i]] = i;
			}
			for(ll o = 0; o < j; o++){
				swap(s[x[o]], s[y[o]]);
				swap(b[s[x[o]]], b[s[y[o]]]);
				while(p2 == Sz(vec[p1])){
					p1++;
					p2 = 1;
				}
				p[o] = b[vec[p1][0]];
				q[o] = b[vec[p1][p2]];
				p2++;
				swap(s[p[o]], s[q[o]]);
				swap(b[s[p[o]]], b[s[q[o]]]);
			}
	/*		cout << j << "\n";
			for(ll i = 0; i < j; i++){
				cout << p[i] << " " << q[i] << "\n";
			}*/
			return j;
}


/*int main(){
    fast_io;



    return 0;
}*/

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:50:39: warning: unused parameter 'm' [-Wunused-parameter]
   50 | int findSwapPairs(int N, int S[], int m, int X[], int Y[], int p[], int q[]) {
      |                                   ~~~~^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 2 ms 492 KB Output is correct
23 Correct 2 ms 492 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 492 KB Output is correct
27 Correct 2 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 620 KB Output is correct
5 Correct 2 ms 620 KB Output is correct
6 Correct 2 ms 620 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 2 ms 492 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 2 ms 620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 620 KB Output is correct
5 Correct 2 ms 620 KB Output is correct
6 Correct 2 ms 620 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 2 ms 492 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 2 ms 620 KB Output is correct
15 Correct 162 ms 13864 KB Output is correct
16 Correct 178 ms 21492 KB Output is correct
17 Correct 191 ms 22912 KB Output is correct
18 Correct 93 ms 28056 KB Output is correct
19 Correct 164 ms 25756 KB Output is correct
20 Correct 175 ms 26012 KB Output is correct
21 Correct 176 ms 25940 KB Output is correct
22 Correct 157 ms 18496 KB Output is correct
23 Correct 178 ms 23536 KB Output is correct
24 Correct 195 ms 23652 KB Output is correct
25 Correct 194 ms 23108 KB Output is correct
26 Correct 172 ms 26140 KB Output is correct
27 Correct 161 ms 26268 KB Output is correct
28 Correct 193 ms 23796 KB Output is correct
29 Correct 187 ms 23584 KB Output is correct
30 Correct 131 ms 28956 KB Output is correct
31 Correct 191 ms 24176 KB Output is correct
32 Correct 181 ms 23512 KB Output is correct
33 Correct 199 ms 23472 KB Output is correct
34 Correct 189 ms 23140 KB Output is correct
35 Correct 164 ms 25244 KB Output is correct
36 Correct 89 ms 30104 KB Output is correct
37 Correct 200 ms 23868 KB Output is correct
38 Correct 187 ms 22908 KB Output is correct
39 Correct 206 ms 23156 KB Output is correct