입력 코드
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int x;
int y;
} point;
point sort[100001];
void merge(point* arr, int first, int mid, int last)
{
int i, j, k;
i = first;
j = mid + 1;
k = first;
while (i <= mid && j <= last) {
if (arr[i].x < arr[j].x)
sort[k++] = arr[i++];
else if (arr[i].x > arr[j].x)
sort[k++] = arr[j++];
else {
if (arr[i].y < arr[j].y)
sort[k++] = arr[i++];
else if (arr[i].y > arr[j].y)
sort[k++] = arr[j++];
}
}
if (i <= mid) {
while (i <= mid)
sort[k++] = arr[i++];
}
else {
while (j <= last)
sort[k++] = arr[j++];
}
for (k = first; k <= last; k++)
arr[k] = sort[k];
}
void mergesort(point* arr, int first, int last) {
int mid;
if (first < last) {
mid = (first + last) / 2;
mergesort(arr, first, mid);
mergesort(arr, mid + 1, last);
merge(arr, first, mid, last);
}
}
int main(void)
{
int n;
scanf("%d", &n);
point* arr = (point*)malloc(sizeof(point)*n);
for(int i = 0; i < n; i++)
scanf("%d %d", &arr[i].x, &arr[i].y);
mergesort(arr, 0, n-1);
for (int i = 0; i < n; i++)
printf("%d %d\n", arr[i].x, arr[i].y);
return 0;
}
출처 daydreamx.tistory.com/entry/baekjoon11650
코드 설명
#정렬
문제 출처
'C' 카테고리의 다른 글
#65. [백준_C언어] 10773 : 제로 \ 스택 (0) | 2021.02.08 |
---|---|
#64. [백준_C언어] 11866 : 요세푸스 문제 0 (0) | 2021.02.03 |
#62. [백준_C언어] 10866 : 덱 (0) | 2021.02.02 |
#61. [백준_C언어] 10845 : 큐 (0) | 2021.02.02 |
#60. [백준_C언어] 10828 : 스택 (0) | 2021.02.01 |