ps:problems:boj:1966
프린터 큐
ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 1966 |
문제명 | 프린터 큐 |
레벨 | 실버 3 |
분류 |
애드혹 |
시간복잡도 | O(T*n) |
인풋사이즈 | T<=???, n<=100 |
사용한 언어 | Python |
제출기록 | 29200KB / 76ms |
최고기록 | 56ms |
해결날짜 | 2021/08/12 |
풀이
- 프린터과 동일한 문제. 풀이는 그쪽을 참조.
코드
"""Solution code for "BOJ 1966. 프린터 큐".
- Problem link: https://www.acmicpc.net/problem/1966
- Solution link: http://www.teferi.net/ps/problems/boj/1966
"""
import sys
def main():
def dist_from_last_index(index):
dist = index - last_index
return dist if dist >= 0 else dist + N
T = int(sys.stdin.readline())
for _ in range(T):
N, M = [int(x) for x in sys.stdin.readline().split()]
priorities = [int(x) for x in sys.stdin.readline().split()]
max_priority = max(priorities)
indexes_by_priority = [[] for _ in range(max_priority + 1)]
for i, p in enumerate(priorities):
indexes_by_priority[p].append(i)
last_index = 0
answer = 0
for p in range(max_priority, priorities[M], -1):
if not (indexes := indexes_by_priority[p]):
continue
answer += len(indexes)
last_index = max(indexes, key=dist_from_last_index)
target_dist = dist_from_last_index(M)
answer += sum(1 for i in indexes_by_priority[priorities[M]]
if dist_from_last_index(i) <= target_dist)
print(answer)
if __name__ == '__main__':
main()
ps/problems/boj/1966.txt · 마지막으로 수정됨: 2021/08/12 09:16 저자 teferi
토론