[PS] BOJ 26123 / 외계 침략자 윤이

[PS] BOJ 26123 / 외계 침략자 윤이
문제 링크: https://www.acmicpc.net/problem/26123
Thumbnail: Photo by Danie Franco (Unsplash)

풀이

​윤이가 레이저를 발사하는 방식을 그대로 구현해주면 됩니다.

맵을 사용해, 각 높이별로 건물의 개수를 기록해두고 $D$일동안 반복하면 됩니다.

전체 코드

input = open(0).readline
N, D = map(int, input().split())
heights = {}
max_height = 0
for height in map(int, input().split()):
    if height > max_height:
        max_height = height
    try:
        heights[height] += 1
    except KeyError:
        heights[height] = 1

count = 0
for day in range(D):
    count += heights[max_height]
    try:
        heights[max_height - 1] += heights[max_height]
    except KeyError:
        if max_height - 1 == 0: # 모든 건물의 높이가 0이 되면 더 이상 레이저를 쏘지 않는다. 
            break
        heights[max_height - 1] = heights[max_height]
    del heights[max_height]
    max_height -= 1

print(count)
    

solution.py