Submission #590883

# Submission time Handle Problem Language Result Execution time Memory
590883 2022-07-06T12:45:18 Z FatihSolak Sorting (IOI15_sorting) C++17
100 / 100
496 ms 20600 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
bool ck(int n,int s[],int m,int x[],int y[]){
	if(is_sorted(s,s+n))
		return 1;
	vector<int> fnal;
	vector<int> now;
	vector<int> pfnal(n,0);
	vector<int> pnow(n,0);
	int cnt = 0;
	for(int i = 0;i<n;i++){
		fnal.push_back(s[i]);
		pfnal[s[i]] = i;
		now.push_back(s[i]);
		pnow[s[i]] = i;
		cnt += (s[i] == i);
	}
	for(int i = 0;i<m;i++){
		swap(pfnal[fnal[x[i]]],pfnal[fnal[y[i]]]);
		swap(fnal[x[i]],fnal[y[i]]);
	}
	int num = 0;
	for(int i = 0;i<m;i++){
		if(pnow[now[x[i]]] == now[x[i]])cnt--;
		if(pnow[now[y[i]]] == now[y[i]])cnt--;
		swap(now[x[i]],now[y[i]]);
		swap(pnow[now[x[i]]],pnow[now[y[i]]]);
		if(pnow[now[x[i]]] == now[x[i]])cnt++;
		if(pnow[now[y[i]]] == now[y[i]])cnt++;
		while(num < n){
			int ps1 = pnow[num];
			int ps2 = pnow[fnal[num]];
			if(ps1 == ps2){
				num++;
				continue;
			}
			if(pnow[now[ps1]] == now[ps1])cnt--;
			if(pnow[now[ps2]] == now[ps2])cnt--;
			swap(pnow[now[ps1]],pnow[now[ps2]]);
			swap(now[ps1],now[ps2]);
			if(pnow[now[ps1]] == now[ps1])cnt++;
			if(pnow[now[ps2]] == now[ps2])cnt++;
			int tmp = pfnal[num];
			swap(pfnal[fnal[num]],pfnal[num]);
			swap(fnal[num],fnal[tmp]);
			num++;
			break;
		}
		if(cnt == n)
			return 1;
	}
	return 0;
}
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
	int l = 0,r = m;
	while(l < r){
		int mm = (l + r)/2;
		if(ck(n,s,mm,x,y)){
			r = mm;
		}
		else l = mm+1;
	}
	m = l;
	// for(int i = n;i>=1;i--){
	// 	if(ck(n,s,i,x,y)){
	// 		m = i;
	// 	}
	// }
	if(is_sorted(s,s+n))
		return 0;
	for(int i = 0;i<m;i++){
		p[i] = q[i] = 0;
	}
	vector<int> fnal;
	vector<int> now;
	vector<int> pfnal(n,0);
	vector<int> pnow(n,0);
	int cnt = 0;
	for(int i = 0;i<n;i++){
		fnal.push_back(s[i]);
		pfnal[s[i]] = i;
		now.push_back(s[i]);
		pnow[s[i]] = i;
		cnt += (s[i] == i);
	}
	for(int i = 0;i<m;i++){
		swap(pfnal[fnal[x[i]]],pfnal[fnal[y[i]]]);
		swap(fnal[x[i]],fnal[y[i]]);
	}
	int num = 0;
	for(int i = 0;i<m;i++){
		if(pnow[now[x[i]]] == now[x[i]])cnt--;
		if(pnow[now[y[i]]] == now[y[i]])cnt--;
		swap(now[x[i]],now[y[i]]);
		swap(pnow[now[x[i]]],pnow[now[y[i]]]);
		if(pnow[now[x[i]]] == now[x[i]])cnt++;
		if(pnow[now[y[i]]] == now[y[i]])cnt++;
		while(num < n){
			int ps1 = pnow[num];
			int ps2 = pnow[fnal[num]];
			if(ps1 == ps2){
				num++;
				continue;
			}
			p[i] = ps1;
			q[i] = ps2;
			if(pnow[now[ps1]] == now[ps1])cnt--;
			if(pnow[now[ps2]] == now[ps2])cnt--;
			swap(pnow[now[ps1]],pnow[now[ps2]]);
			swap(now[ps1],now[ps2]);
			if(pnow[now[ps1]] == now[ps1])cnt++;
			if(pnow[now[ps2]] == now[ps2])cnt++;
			int tmp = pfnal[num];
			swap(pfnal[fnal[num]],pfnal[num]);
			swap(fnal[num],fnal[tmp]);
			num++;
			break;
		}
		if(cnt == n)
			return i+1;
	}
	assert(0);
}


# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 292 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 292 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 2 ms 432 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 3 ms 340 KB Output is correct
3 Correct 2 ms 392 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 3 ms 340 KB Output is correct
3 Correct 2 ms 392 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 325 ms 10704 KB Output is correct
16 Correct 383 ms 18872 KB Output is correct
17 Correct 496 ms 19736 KB Output is correct
18 Correct 45 ms 13420 KB Output is correct
19 Correct 341 ms 19332 KB Output is correct
20 Correct 327 ms 19960 KB Output is correct
21 Correct 337 ms 20044 KB Output is correct
22 Correct 307 ms 14984 KB Output is correct
23 Correct 332 ms 20308 KB Output is correct
24 Correct 467 ms 20124 KB Output is correct
25 Correct 468 ms 19976 KB Output is correct
26 Correct 330 ms 19896 KB Output is correct
27 Correct 303 ms 19344 KB Output is correct
28 Correct 434 ms 20056 KB Output is correct
29 Correct 440 ms 19756 KB Output is correct
30 Correct 221 ms 19220 KB Output is correct
31 Correct 443 ms 20156 KB Output is correct
32 Correct 343 ms 19672 KB Output is correct
33 Correct 481 ms 20052 KB Output is correct
34 Correct 403 ms 19920 KB Output is correct
35 Correct 325 ms 19108 KB Output is correct
36 Correct 108 ms 18964 KB Output is correct
37 Correct 496 ms 20600 KB Output is correct
38 Correct 456 ms 19916 KB Output is correct
39 Correct 475 ms 20032 KB Output is correct