예제 입력
5 0 4 1 2 1 -1 2 2 3 3
예제 출력
1 -1 1 2 2 2 3 3 0 4
y를 오름차순으로 정렬하는데, y가 같으면 x의 오름차순으로 정렬하는 문제.
quick sort를 사용하면 nlgn인데,
y, x를 둘다하게되면
시간초과여서
꼼수를 부렸다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <vector> | |
/** | |
* https://www.acmicpc.net/problem/11651 | |
* BOJ 백준온라인져지 11651 좌표 정렬하기 2 풀이 | |
*/ | |
using namespace std; | |
void quickSort(vector<long> &arr, int left, int right) { | |
int i = left, j = right; | |
long pivot = arr[(left + right) / 2]; | |
long temp; | |
do { | |
while (arr[i] < pivot) | |
i++; | |
while (arr[j] > pivot) | |
j--; | |
if (i <= j) { | |
temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp; | |
i++; | |
j--; | |
} | |
} while (i <= j); | |
/* recursion */ | |
if (left < j) | |
quickSort(arr, left, j); | |
if (i < right) | |
quickSort(arr, i, right); | |
} | |
int main(){ | |
int T; | |
scanf("%d",&T); | |
vector<long> pointList; | |
while(T--){ | |
long x1,y1; | |
scanf("%ld%ld",&x1,&y1); | |
pointList.push_back((y1+100000)*1000000+(x1+100000)); | |
} | |
quickSort(pointList, 0, pointList.size()-1); | |
for(int i = 0, size = pointList.size(); i<size; i++){ | |
long y = pointList[i] / 1000000 - 100000; | |
long x = pointList[i] % 1000000 - 100000; | |
printf("%ld %ld\n",x,y); | |
} | |
return 0; | |
} |
'IT > 알고리즘' 카테고리의 다른 글
BOJ 백준온라인져지 1085 직사각형에서 탈출 풀이 (0) | 2017.12.01 |
---|---|
Quick Sort (0) | 2017.11.30 |
BOJ 백준온라인져지 1004 어린 왕자 풀이 (0) | 2017.11.30 |
BOJ 백준온라인져지 1002 터렛 풀이 (0) | 2017.11.30 |
BOJ 백준온라인져지 11866 조세퍼스 문제 0 풀이 (0) | 2017.11.29 |