답안 #434024

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
434024 2021-06-20T14:10:26 Z pliam 정렬하기 (IOI15_sorting) C++14
100 / 100
544 ms 23372 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
#define MAXN 200005

int from[MAXN];
int from_pos[MAXN];
int s_pos[MAXN];
int n;
int s[MAXN];
int* x;
int* y;
int p[MAXN];
int q[MAXN];

void change_from(int a,int b){
	swap(from_pos[a],from_pos[b]);
	from[from_pos[a]]=a;
	from[from_pos[b]]=b;
}

void swap_s(int a,int b){
	swap(s[a],s[b]);
	s_pos[s[a]]=a;
	s_pos[s[b]]=b;
}

bool check(int r){
	for(int i=0;i<n;i++){
		from[i]=i;
		from_pos[i]=i;
		s_pos[s[i]]=i;
	}
	for(int i=r-1;i>0;i--){
		if(x[i]!=y[i]) change_from(x[i],y[i]);
	}
	int i=0;
	for(int pos=0;pos<r;pos++){
		if(x[pos]!=y[pos]) swap_s(x[pos],y[pos]);

		while(i<n){
			if(s_pos[i]!=from[i]) break;
			i++;
		}
		if(i!=n){
			p[pos]=from[i];
			q[pos]=s_pos[i];
			swap_s(p[pos],q[pos]);
			i++;
		}else{
			p[pos]=0;
			q[pos]=0;
		}
		if((pos<r-1)&&(x[pos+1]!=y[pos+1])) change_from(x[pos+1],y[pos+1]);
	}
	for(int i=0;i<n;i++){
		if(s[i]!=i) return false;
	}
	return true;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	n=N;
	x=X;
	y=Y;
	for(int i=0;i<n;i++){
		s[i]=S[i];
	}
	bool sorted=true;
    for(int i=0;i<n;i++){
		if(s[i]!=i) sorted=false;
	}
	if(sorted) return 0;

	int k=N;
	for(int b=N-1;b>0;b/=2){
		while(k-b>0){
			bool ans=check(k-b);
			for(int i=0;i<n;i++){
				s[i]=S[i];
			}
			if(!ans) break;
			k-=b;
		}
	}
	check(k);
	for(int i=0;i<k;i++){
		P[i]=p[i];
		Q[i]=q[i];
	}
	return k;
}


Compilation message

sorting.cpp: In function 'bool check(int)':
sorting.cpp:56:10: warning: declaration of 'i' shadows a previous local [-Wshadow]
   56 |  for(int i=0;i<n;i++){
      |          ^
sorting.cpp:37:6: note: shadowed declaration is here
   37 |  int i=0;
      |      ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:62:39: warning: unused parameter 'M' [-Wunused-parameter]
   62 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 1 ms 588 KB Output is correct
25 Correct 1 ms 460 KB Output is correct
26 Correct 1 ms 460 KB Output is correct
27 Correct 1 ms 568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 2 ms 440 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 3 ms 444 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 2 ms 440 KB Output is correct
7 Correct 3 ms 460 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 3 ms 444 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 270 ms 20676 KB Output is correct
16 Correct 341 ms 21172 KB Output is correct
17 Correct 474 ms 22252 KB Output is correct
18 Correct 48 ms 14076 KB Output is correct
19 Correct 341 ms 19900 KB Output is correct
20 Correct 421 ms 20988 KB Output is correct
21 Correct 381 ms 21020 KB Output is correct
22 Correct 226 ms 17640 KB Output is correct
23 Correct 279 ms 23148 KB Output is correct
24 Correct 423 ms 22828 KB Output is correct
25 Correct 456 ms 22528 KB Output is correct
26 Correct 297 ms 20548 KB Output is correct
27 Correct 280 ms 19908 KB Output is correct
28 Correct 544 ms 22428 KB Output is correct
29 Correct 492 ms 21908 KB Output is correct
30 Correct 165 ms 19244 KB Output is correct
31 Correct 528 ms 22304 KB Output is correct
32 Correct 298 ms 22344 KB Output is correct
33 Correct 468 ms 22716 KB Output is correct
34 Correct 400 ms 22784 KB Output is correct
35 Correct 312 ms 19780 KB Output is correct
36 Correct 61 ms 18244 KB Output is correct
37 Correct 481 ms 23372 KB Output is correct
38 Correct 439 ms 22448 KB Output is correct
39 Correct 423 ms 22604 KB Output is correct