Submission #640218

# Submission time Handle Problem Language Result Execution time Memory
640218 2022-09-14T02:17:13 Z ggoh Sorting (IOI15_sorting) C++14
100 / 100
409 ms 12124 KB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
#define sz(v) ((int)(v).size())
typedef long long lint;
 
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    int p=-1,q=min(M,N),h,st[200002],sz=0;;
	vector<int>C(N),R(N),go(N),rev(N);
	while(p!=q-1)
	{
		h=(p+q)/2;
		for(int i=0;i<N;i++)C[i]=S[i];
		for(int i=0;i<h;i++)swap(C[X[i]],C[Y[i]]);
		sz=0;
		for(int i=0;i<N;i++)
		{
			go[C[i]]=i;
			rev[i]=C[i];
			if(go[C[i]]!=C[i])
			{
				st[sz++]=C[i];
			}
		}
		for(int i=0;i<N;i++)
		{
			C[i]=S[i];
			R[S[i]]=i;
		}
		for(int i=0;i<h;i++)
		{
			swap(R[C[X[i]]],R[C[Y[i]]]);
			swap(C[X[i]],C[Y[i]]);
			while(sz>0 && go[st[sz-1]]==st[sz-1])sz--;
			if(sz>0)
			{
				int ch=st[sz-1];
				int j=rev[ch];
				sz--;
				swap(C[R[j]],C[R[ch]]);
				swap(R[j],R[ch]);
				swap(rev[go[j]],rev[go[ch]]);
				swap(go[j],go[ch]);
			}
		}
		int s=0;
		for(int i=0;i<N;i++)if(go[i]==i)s++;
		if(s==N)q=h;
		else p=h;
	}
 
	for(int i=0;i<N;i++)C[i]=S[i];
	for(int i=0;i<q;i++)swap(C[X[i]],C[Y[i]]);
	sz=0;
	for(int i=0;i<N;i++)
	{
		go[C[i]]=i;
		rev[i]=C[i];
		if(go[C[i]]!=C[i])st[sz++]=C[i];
	}
	for(int i=0;i<N;i++)
	{
		C[i]=S[i];
		R[S[i]]=i;
	}
	for(int i=0;i<q;i++)
	{
		swap(R[C[X[i]]],R[C[Y[i]]]);
		swap(C[X[i]],C[Y[i]]);
		while(sz>0 && go[st[sz-1]]==st[sz-1])sz--;
		if(sz>0)
		{
			int ch=st[sz-1];
			int j=rev[ch];
			sz--;
			swap(C[R[j]],C[R[ch]]);
			swap(R[j],R[ch]);
			P[i]=R[j],Q[i]=R[ch];
			swap(rev[go[j]],rev[go[ch]]);
			swap(go[j],go[ch]);
		}
		else P[i]=Q[i]=0;
	}
	return q;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 1056 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 1064 KB Output is correct
7 Correct 1 ms 1088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 1056 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 1064 KB Output is correct
7 Correct 1 ms 1088 KB Output is correct
8 Correct 1 ms 980 KB Output is correct
9 Correct 1 ms 980 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 1056 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 980 KB Output is correct
6 Correct 1 ms 1064 KB Output is correct
7 Correct 1 ms 1088 KB Output is correct
8 Correct 1 ms 980 KB Output is correct
9 Correct 1 ms 980 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 1 ms 1108 KB Output is correct
16 Correct 1 ms 1108 KB Output is correct
17 Correct 1 ms 1108 KB Output is correct
18 Correct 1 ms 980 KB Output is correct
19 Correct 1 ms 980 KB Output is correct
20 Correct 1 ms 980 KB Output is correct
21 Correct 2 ms 1236 KB Output is correct
22 Correct 2 ms 1200 KB Output is correct
23 Correct 2 ms 1236 KB Output is correct
24 Correct 2 ms 1208 KB Output is correct
25 Correct 2 ms 1248 KB Output is correct
26 Correct 1 ms 1208 KB Output is correct
27 Correct 1 ms 1204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1204 KB Output is correct
2 Correct 2 ms 1236 KB Output is correct
3 Correct 2 ms 1236 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 2 ms 1208 KB Output is correct
6 Correct 2 ms 1108 KB Output is correct
7 Correct 2 ms 1236 KB Output is correct
8 Correct 2 ms 1144 KB Output is correct
9 Correct 2 ms 1236 KB Output is correct
10 Correct 2 ms 1236 KB Output is correct
11 Correct 2 ms 1248 KB Output is correct
12 Correct 2 ms 1236 KB Output is correct
13 Correct 2 ms 1236 KB Output is correct
14 Correct 1 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1204 KB Output is correct
2 Correct 2 ms 1236 KB Output is correct
3 Correct 2 ms 1236 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 2 ms 1208 KB Output is correct
6 Correct 2 ms 1108 KB Output is correct
7 Correct 2 ms 1236 KB Output is correct
8 Correct 2 ms 1144 KB Output is correct
9 Correct 2 ms 1236 KB Output is correct
10 Correct 2 ms 1236 KB Output is correct
11 Correct 2 ms 1248 KB Output is correct
12 Correct 2 ms 1236 KB Output is correct
13 Correct 2 ms 1236 KB Output is correct
14 Correct 1 ms 1236 KB Output is correct
15 Correct 297 ms 10248 KB Output is correct
16 Correct 359 ms 11300 KB Output is correct
17 Correct 390 ms 11516 KB Output is correct
18 Correct 95 ms 9828 KB Output is correct
19 Correct 264 ms 11084 KB Output is correct
20 Correct 293 ms 11372 KB Output is correct
21 Correct 268 ms 11364 KB Output is correct
22 Correct 303 ms 11236 KB Output is correct
23 Correct 318 ms 12124 KB Output is correct
24 Correct 395 ms 11816 KB Output is correct
25 Correct 347 ms 11632 KB Output is correct
26 Correct 279 ms 11372 KB Output is correct
27 Correct 243 ms 10960 KB Output is correct
28 Correct 384 ms 11780 KB Output is correct
29 Correct 312 ms 11676 KB Output is correct
30 Correct 188 ms 10828 KB Output is correct
31 Correct 355 ms 11764 KB Output is correct
32 Correct 316 ms 11448 KB Output is correct
33 Correct 338 ms 11612 KB Output is correct
34 Correct 363 ms 11652 KB Output is correct
35 Correct 247 ms 10808 KB Output is correct
36 Correct 83 ms 10256 KB Output is correct
37 Correct 409 ms 12060 KB Output is correct
38 Correct 370 ms 11488 KB Output is correct
39 Correct 364 ms 11432 KB Output is correct