-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11650.cpp
More file actions
110 lines (87 loc) · 2.13 KB
/
11650.cpp
File metadata and controls
110 lines (87 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<iostream>
#include<cstdio>
using namespace std;
void swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
}
int partion(int** &arr, int left, int right){
int r1 = left;
int r2 = (left+right)/2;
int r3 = right;
int max, min;
if(arr[r1][0]>arr[r2][0]&&arr[r1][0]>arr[r3][0]){
max = r1;
}else if(arr[r2][0]>arr[r1][0]&&arr[r2][0]>arr[r3][0]){
max = r2;
}else{
max = r3;
}
if(arr[r1][0]<arr[r2][0]&&arr[r1][0]<arr[r3][0]){
min = r1;
}else if(arr[r2][0]<arr[r1][0]&&arr[r2][0]<arr[r3][0]){
min = r2;
}else{
min = r3;
}
int r = (r1+r2+r3) - min - max;
swap(arr[left][0], arr[r][0]);
swap(arr[left][1], arr[r][1]);
int* pivot = arr[left];
int i = left, j = right;
while(i<j){
while(pivot[0]>=arr[i][0]){
if(pivot[0]==arr[i][0]){
if(pivot[1]>arr[i][1]){
i++;
}else{
break;
}
}else{
i++;
}
}
while(pivot[0]<=arr[j][0]){
if(pivot[0]==arr[j][0]){
if(pivot[1]<arr[j][1]){
j--;
}else{
break;
}
}else{
j--;
}
}
swap(arr[i][0], arr[j][0]);
swap(arr[i][1], arr[j][1]);
}
swap(arr[i][0], arr[left][0]);
swap(arr[i][1], arr[left][1]);
return i;
}
void quickSort(int** &arr, int left, int right){
if(left>=right){
return;
}
int p = partion(arr, left, right);
quickSort(arr, left, p-1);
quickSort(arr, p+1, right);
}
int main(){
// cin.tie(NULL);
// ios_base::sync_with_stdio(false);
int N, temp;
cin>>N;
int** coorArr = new int*[N];
for(int i=0; i<N; i++){
coorArr[i] = new int[2];
scanf("%d %d", &coorArr[i][0], &coorArr[i][1]);
}
quickSort(coorArr, 0, N-1);
for(int i=0; i<N; i++){
printf("%d %d\n", coorArr[i][0], coorArr[i][1]);
}
//cin>>N;
delete[] coorArr;
}