| ps | |
|---|---|
| 링크 | acmicpc.net/… |
| 출처 | BOJ |
| 문제 번호 | 1700 |
| 문제명 | 멀티탭 스케줄링 |
| 레벨 | 골드 1 |
| 분류 |
그리디 |
| 시간복잡도 | O(mlogn) |
| 인풋사이즈 | m<=100, n<=100 |
| 사용한 언어 | Python 3.11 |
| 제출기록 | 31256KB / 40ms |
| 최고기록 | 36ms |
| 해결날짜 | 2022/01/13 |
"""Solution code for "BOJ 1700. 멀티탭 스케줄링".
- Problem link: https://www.acmicpc.net/problem/1700
- Solution link: http://www.teferi.net/ps/problems/boj/1700
Tags: [Greedy]
"""
INF = float('inf')
def main():
N, K = [int(x) for x in input().split()]
devices = [int(x) - 1 for x in input().split()]
times_by_device = [[INF] for _ in range(K)]
for time, device in reversed(list(enumerate(devices))):
times_by_device[device].append(time)
plugged = set()
answer = 0
for device in devices:
times_by_device[device].pop()
if device not in plugged and len(plugged) == N:
device_to_unplug = max(
plugged, key=lambda device: times_by_device[device][-1])
plugged.remove(device_to_unplug)
answer += 1
plugged.add(device)
print(answer)
if __name__ == '__main__':
main()
"""Solution code for "BOJ 1700. 멀티탭 스케줄링".
- Problem link: https://www.acmicpc.net/problem/1700
- Solution link: http://www.teferi.net/ps/problems/boj/1700
Tags: [Greedy]
"""
from teflib import priorityqueue
INF = float('inf')
def main():
N, K = [int(x) for x in input().split()]
devices = [int(x) - 1 for x in input().split()]
times_by_device = [[INF] for _ in range(K)]
for time, device in zip(range(K, 0, -1), reversed(devices)):
times_by_device[device].append(time)
plugged_devices = priorityqueue.PriorityDict()
answer = 0
for device in devices:
if device not in plugged_devices and len(plugged_devices) == N:
plugged_devices.pop_min()
answer += 1
times_by_device[device].pop()
plugged_devices[device] = -times_by_device[device][-1]
print(answer)
if __name__ == '__main__':
main()