문제
한 개의 회의실이 있는데 이를 사용하고자 하는 N개의 회의에 대하여 회의실 사용표를 만들려고 한다. 각 회의 I에 대해 시작시간과 끝나는 시간이 주어져 있고, 각 회의가 겹치지 않게 하면서 회의실을 사용할 수 있는 회의의 최대 개수를 찾아보자. 단, 회의는 한번 시작하면 중간에 중단될 수 없으며 한 회의가 끝나는 것과 동시에 다음 회의가 시작될 수 있다. 회의의 시작시간과 끝나는 시간이 같을 수도 있다. 이 경우에는 시작하자마자 끝나는 것으로 생각하면 된다.
입력
첫째 줄에 회의의 수 N(1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N+1 줄까지 각 회의의 정보가 주어지는데 이것은 공백을 사이에 두고 회의의 시작시간과 끝나는 시간이 주어진다. 시작 시간과 끝나는 시간은 231-1보다 작거나 같은 자연수 또는 0이다.
출력
첫째 줄에 최대 사용할 수 있는 회의의 최대 개수를 출력한다.
예제 입력 1 복사
11
1 4
3 5
0 6
5 7
3 8
5 9
6 10
8 11
8 12
2 13
12 14
예제 출력 1 복사
4
문제 요약
N개의 회의가 겹치지않고 가장 많이 할 수 있는 갯수
접근 방식
1. 끝나는 시간이 짧을 수록 더욱 많은 것이 가능
1
2
3
4
5
6
7
8
9
10
11
12
|
N = int(input())
result = [list(map(int, input().split())) for _ in range(N)]
result.sort(key=lambda x: (x[1], x[0]))
cnt = 1
end = result[0][1]
for i in range(1,N):
if result[i][0] >= end:
cnt += 1
end = result[i][1]
print(cnt)
|
2. sort가 아닌 merge로
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
|
N = int(input())
result = [list(map(int, input().split())) for _ in range(N)]
temp = [0 for _ in range(N)]
def merge(start, end):
if start >= end:
return
mid = (start+end) //2
merge(start, mid)
merge(mid+1,end)
i = start
j = mid+1
tempIndex = i
while i <= mid and j <= end:
if result[i][1] > result[j][1]:
temp[tempIndex] = result[j]
j+=1
elif result[i][1] < result[j][1]:
temp[tempIndex] = result[i]
i += 1
else:
if result[i][0] > result[j][0]:
temp[tempIndex] = result[j]
j += 1
else:
temp[tempIndex] = result[i]
i += 1
tempIndex += 1
if i <= mid:
for index in result[i:mid+1]:
temp[tempIndex] = index
tempIndex += 1
elif j <= end:
for index in result[j:end+1]:
temp[tempIndex] = index
tempIndex += 1
result[start:end+1] = temp[start:end+1]
merge(0, N-1)
cnt = 1
end = result[0][1]
for i in range(1,N):
if result[i][0] >= end:
cnt += 1
end = result[i][1]
print(cnt)
|
'알고리즘 문제 > 백준' 카테고리의 다른 글
알고리즘 - 1541 잃어버린 괄호 (0) | 2022.10.02 |
---|---|
알고리즘 - 11399 ATM (0) | 2022.10.02 |
알고리즘 - 11047 (동전 0) (0) | 2022.10.01 |
알고리즘 - 동적계획법 (12865 평범한 배낭) (0) | 2022.09.12 |
알고리즘 - 동적계획법 (9251 LCS) (0) | 2022.09.12 |