목차

멀티탭 스케줄링

ps
링크acmicpc.net/…
출처BOJ
문제 번호1700
문제명멀티탭 스케줄링
레벨골드 1
분류

그리디

시간복잡도O(mlogn)
인풋사이즈m<=100, n<=100
사용한 언어Python 3.11
제출기록31256KB / 40ms
최고기록36ms
해결날짜2022/01/13

풀이

코드

코드 1 - O(MN)

"""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()

코드 2 - O(MlogN)

"""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()