처음에 선택정렬을 사용 => 시간초과
퀵 소트 사용 => 메모리초과
퀵 소트 특성상 마지막에 함수들을 부른다.
머지 소트 => 성공
머지소트는 먼저 함수들을 불러서 메모리가 적게든다.
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> | |
/** | |
* https://www.acmicpc.net/problem/1931 | |
* BOJ 백준온라인져지 1931 회의실배정 풀이 | |
*/ | |
struct Time{ | |
int startTime, endTime; | |
}; | |
bool isBiggerAThanB(Time &A, Time &B){ | |
if(A.endTime == B.endTime) | |
return A.startTime > B.startTime; | |
return A.endTime > B.endTime; | |
} | |
Time timeList[100001]; | |
Time tempTimeList[100001]; | |
void mergeSort(int l, int r){ | |
if(l < r){ | |
int mid = (l + r) / 2; | |
mergeSort(l, mid); | |
mergeSort(mid + 1, r); | |
int i = l, j = mid + 1, k = l, cnt = 0; | |
while(i <= mid && j <= r){ | |
if(isBiggerAThanB(timeList[i], timeList[j])) | |
tempTimeList[k++] = timeList[j++]; | |
else | |
tempTimeList[k++] = timeList[i++]; | |
} | |
while(i <= mid){ | |
tempTimeList[k++] = timeList[i++]; | |
} | |
while(j <= r){ | |
tempTimeList[k++] = timeList[j++]; | |
} | |
while(l <= r){ | |
timeList[l] = tempTimeList[l]; | |
l++; | |
} | |
} | |
} | |
int main(){ | |
int N; | |
scanf("%d", &N); | |
for(int i = 0; i < N; i++){ | |
scanf("%d%d", &timeList[i].startTime, &timeList[i].endTime); | |
} | |
mergeSort(0, N - 1); | |
int result = 0, endTime = -1; | |
for(int i = 0; i < N; i++){ | |
if(timeList[i].startTime >= endTime){ | |
result++, endTime = timeList[i].endTime; | |
} | |
} | |
printf("%d", result); | |
} |
'IT > 알고리즘' 카테고리의 다른 글
BOJ 백준온라인져지 6376 e 계산 풀이 (0) | 2017.12.11 |
---|---|
BOJ 백준온라인져지 2217 로프 풀이 (0) | 2017.12.10 |
BOJ 백준온라인져지 13241 최소공배수 풀이 (0) | 2017.12.08 |
BOJ 백준온라인져지 1977 완전제곱수 풀이 (0) | 2017.12.08 |
BOJ 백준온라인져지 10826 피보나치 수 4 풀이 (0) | 2017.12.08 |