답안 #119283

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
119283 2019-06-20T22:21:34 Z eohomegrownapps 정렬하기 (IOI15_sorting) C++14
100 / 100
193 ms 21028 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;

int howManySwaps(int check, int N, vector<int> S, int* X, int* Y, vector<int> el2ind){
	//cout<<"how many "<<check<<endl;
	for (int i = 0; i<check; i++){
		int tmp = S[X[i]];
		S[X[i]]=S[Y[i]];
		S[Y[i]]=tmp;
		tmp = el2ind[S[Y[i]]];
		el2ind[S[Y[i]]]=el2ind[S[X[i]]];
		el2ind[S[X[i]]]=tmp;
		/*for (int t=0; t<N; t++){
		cout<<S[t]<<" ";
	}cout<<endl;*/
	}
	//cout<<"done"<<endl;
	int swaps = 0;
	/*for (int t=0; t<N; t++){
		cout<<S[t]<<" ";
	}cout<<endl;*/
	for (int i = 0; i<N; i++){
		if (S[i]==i){
			continue;
		}
		//swapping S[i] and S[el2ind[i]]
		int a = el2ind[S[el2ind[i]]], b = el2ind[S[i]], c=S[i], d=S[el2ind[i]];
		int tmp = S[i];
		S[i]=i;
		S[el2ind[i]]=tmp;
		el2ind[c]=a;
		el2ind[d]=b;
		swaps++;
		/*for (int t=0; t<N; t++){
		cout<<S[t]<<" ";
	}cout<<endl;*/
	}
	//cout<<swaps<<endl;
	return swaps;
}

vector<pair<int,int> > answer;

void pushToAns(int check, int N, vector<int> S, int* X, int* Y, vector<int> el2ind){
	//cout<<"how many "<<check<<endl;
	for (int i = 0; i<check; i++){
		int tmp = S[X[i]];
		S[X[i]]=S[Y[i]];
		S[Y[i]]=tmp;
		tmp = el2ind[S[Y[i]]];
		el2ind[S[Y[i]]]=el2ind[S[X[i]]];
		el2ind[S[X[i]]]=tmp;
	}
	int swaps = 0;
	/*for (int t=0; t<N; t++){
		cout<<S[t]<<" ";
	}cout<<endl;*/
	for (int i = 0; i<N; i++){
		if (S[i]==i){
			continue;
		}
		answer.push_back(make_pair(i,el2ind[i]));
		//swapping S[i] and S[el2ind[i]]
		int a = el2ind[S[el2ind[i]]], b = el2ind[S[i]], c=S[i], d=S[el2ind[i]];
		int tmp = S[i];
		S[i]=i;
		S[el2ind[i]]=tmp;
		el2ind[c]=a;
		el2ind[d]=b;
		swaps++;
		/*for (int t=0; t<N; t++){
		cout<<S[t]<<" ";
	}cout<<endl;*/
	}
	//cout<<swaps<<endl;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	vector<int> seq(S,S+N);
	vector<int> el2ind(N,0);
	for (int i = 0; i<N; i++){
		el2ind[seq[i]]=i;
	}
	//binary search
	int l = 0, r = M;
	//find first element with success
	int count,mid;
	while (l<=r){
		//cout<<l<<" "<<r<<endl;
		mid = (l+r)/2;
		count = howManySwaps(mid,N,seq,X,Y,el2ind);
		//cout<<count<<" "<<mid<<endl;
		if (count<=mid){
			r=mid-1;
		} else {
			l=mid+1;
		}
	}
	pushToAns(l,N,seq,X,Y,el2ind);
	//cout<<"answer"<<endl;
	/*for (auto a : answer){
		cout<<a.first<<" "<<a.second<<endl;
	}*/
	if (count<l){
		for (int i = count; i<l; i++){
			answer.push_back(make_pair(0,0));
		}
	}

	//for (auto a : answer){
	//	cout<<a.first<<" "<<a.second<<endl;
	//}
	//cout<<l<<endl;
	vector<int> swapmap(N);
	for (int i = 0; i<N; i++){
		el2ind[i]=i;
		swapmap[i]=i;
	}
	if (l==0){
		return 0;
	}
	P[l-1]=answer[l-1].first;
	Q[l-1]=answer[l-1].second;
	//cout<<"format"<<endl;
	for (auto a = l-2; a>=0; a--){
		//X[a], Y[a] swapped
		//cout<<X[a+1]<<" "<<Y[a+1]<<endl;
		int ia = el2ind[X[a+1]];
		int ib = el2ind[Y[a+1]];
		//cout<<ia<<" "<<ib<<endl;
		int tmp = swapmap[ia];
		swapmap[ia]=swapmap[ib];
		swapmap[ib]=tmp;
		
		tmp = el2ind[X[a+1]];
		el2ind[X[a+1]]=el2ind[Y[a+1]];
		el2ind[Y[a+1]]=tmp;
		/*for (int t=0; t<N; t++){
			cout<<swapmap[t]<<" ";
		}cout<<endl;
		for (int t=0; t<N; t++){
			cout<<el2ind[t]<<" ";
		}cout<<endl;*/
		P[a]=swapmap[answer[a].first];
		Q[a]=swapmap[answer[a].second];
	}
	return l;
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:105:2: warning: 'count' may be used uninitialized in this function [-Wmaybe-uninitialized]
  if (count<l){
  ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 256 KB Output is correct
20 Correct 1 ms 256 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 3 ms 512 KB Output is correct
26 Correct 2 ms 640 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 3 ms 640 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 3 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 568 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 3 ms 640 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 3 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 3 ms 568 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
15 Correct 116 ms 18684 KB Output is correct
16 Correct 138 ms 19172 KB Output is correct
17 Correct 172 ms 20072 KB Output is correct
18 Correct 69 ms 16548 KB Output is correct
19 Correct 127 ms 19168 KB Output is correct
20 Correct 140 ms 19704 KB Output is correct
21 Correct 133 ms 19632 KB Output is correct
22 Correct 105 ms 15360 KB Output is correct
23 Correct 135 ms 20808 KB Output is correct
24 Correct 179 ms 20588 KB Output is correct
25 Correct 173 ms 20304 KB Output is correct
26 Correct 133 ms 19596 KB Output is correct
27 Correct 116 ms 19324 KB Output is correct
28 Correct 159 ms 20548 KB Output is correct
29 Correct 155 ms 20300 KB Output is correct
30 Correct 104 ms 18884 KB Output is correct
31 Correct 158 ms 20588 KB Output is correct
32 Correct 126 ms 20316 KB Output is correct
33 Correct 181 ms 20492 KB Output is correct
34 Correct 144 ms 20452 KB Output is correct
35 Correct 123 ms 18808 KB Output is correct
36 Correct 60 ms 17752 KB Output is correct
37 Correct 164 ms 21028 KB Output is correct
38 Correct 168 ms 20308 KB Output is correct
39 Correct 193 ms 20404 KB Output is correct