Submission #572019

# Submission time Handle Problem Language Result Execution time Memory
572019 2022-06-03T09:48:20 Z CSQ31 Sorting (IOI15_sorting) C++17
100 / 100
169 ms 20524 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
//let K_i = n-number of cycles at step i
//f(i) = max(i,K_i) is non decreasing
const int MAXN = 2e5+1;
bool vis[MAXN];
int p[MAXN],q[MAXN];
void change(int i,int j){
	int a = p[i];
	int b = p[j];
	q[a] = j;
	q[b] = i;
	swap(p[i],p[j]);
}
int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	vector<int>s(n);
	int idx = 0;
	bool ok = 1;
	for(int i=0;i<n;i++)if(S[i] != i)ok = 0;
	if(ok)return 0;
	for(int i=0;i<n;i++)s[i] = S[i];
	int l = 0,r = M-1;
	while(r>=l){
		int mid = (l+r)/2;
		for(int i=0;i<n;i++)S[i] = s[i];
		for(int i=0;i<=mid;i++)swap(S[X[i]],S[Y[i]]);
		int cost = n;
		for(int j=0;j<n;j++)vis[j] = 0;
		for(int j=0;j<n;j++){
			int cur = j;
			if(vis[j])continue;
			while(!vis[cur]){
				vis[cur] = 1;
				cur = S[cur];
			}
			cost--;
		}
		if(cost > mid+1)l = mid+1;
		else r = mid-1;
	}
	idx = l;
	for(int i=0;i<n;i++)S[i] = s[i];
	for(int i=0;i<=idx;i++)swap(S[X[i]],S[Y[i]]);
	vector<array<int,2>>need;
	for(int j=0;j<n;j++)vis[j] = 0;
	for(int j=0;j<n;j++){
		int cur = j;
		if(vis[j])continue;
		while(!vis[cur]){
			vis[cur] = 1;
			cur = S[cur];
			need.push_back({cur,S[cur]});
		}
		need.pop_back();
	}
	//for(auto x:need)cout<<x[0]<<" "<<x[1]<<'\n';
	for(int i=0;i<n;i++){
		p[i] = s[i];
		q[s[i]] = i;
	}
	reverse(need.begin(),need.end());
	for(int i=0;i<=idx;i++){
		change(X[i],Y[i]);
		if(need.empty()){
			P[i] = Q[i] = 0;
			continue;
		}
		//for(int i=0;i<n;i++)cout<<p[i]<<" ";
		//cout<<'\n';
		int v = need.back()[0];
		int u = need.back()[1];
		P[i] = q[v];
		Q[i] = q[u];
		change(q[v],q[u]);
		need.pop_back();
		//for(int i=0;i<n;i++)cout<<p[i]<<" ";
		//cout<<'\n';
	}
	//for(int i=0;i<n;i++)cout<<p[i]<<" ";
   // cout<<'\n';
	return idx+1;
}


# 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 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 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 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 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 1 ms 212 KB Output is correct
2 Correct 1 ms 304 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 212 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 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 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 1 ms 212 KB Output is correct
14 Correct 1 ms 304 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 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 520 KB Output is correct
24 Correct 1 ms 452 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 2 ms 452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 520 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 520 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 142 ms 17928 KB Output is correct
16 Correct 135 ms 18360 KB Output is correct
17 Correct 157 ms 19340 KB Output is correct
18 Correct 42 ms 14172 KB Output is correct
19 Correct 122 ms 18284 KB Output is correct
20 Correct 132 ms 18896 KB Output is correct
21 Correct 125 ms 18880 KB Output is correct
22 Correct 126 ms 14652 KB Output is correct
23 Correct 147 ms 20016 KB Output is correct
24 Correct 144 ms 19764 KB Output is correct
25 Correct 160 ms 19580 KB Output is correct
26 Correct 130 ms 18796 KB Output is correct
27 Correct 119 ms 18232 KB Output is correct
28 Correct 142 ms 19676 KB Output is correct
29 Correct 169 ms 19268 KB Output is correct
30 Correct 90 ms 17832 KB Output is correct
31 Correct 154 ms 19676 KB Output is correct
32 Correct 147 ms 19344 KB Output is correct
33 Correct 152 ms 19756 KB Output is correct
34 Correct 158 ms 19744 KB Output is correct
35 Correct 128 ms 17988 KB Output is correct
36 Correct 67 ms 16868 KB Output is correct
37 Correct 159 ms 20524 KB Output is correct
38 Correct 166 ms 19512 KB Output is correct
39 Correct 151 ms 19880 KB Output is correct