얼렁뚱땅 백준 문제풀이

[백준 문제풀이] 얼렁뚱땅 2304 창고 다각형 풀이

MOSTAR 2022. 10. 6. 14:29

https://www.acmicpc.net/problem/2304

 

2304번: 창고 다각형

첫 줄에는 기둥의 개수를 나타내는 정수 N이 주어진다. N은 1 이상 1,000 이하이다. 그 다음 N 개의 줄에는 각 줄에 각 기둥의 왼쪽 면의 위치를 나타내는 정수 L과 높이를 나타내는 정수 H가 한 개의

www.acmicpc.net

 

n = int(input())
arr = [list(map(int,input().split())) for _ in range(n)]
arr.sort(key=lambda x:x[0])

wow = list(list(zip(*arr))[1])
max_ = max(wow)
max_idx = wow.index(max_)

left = arr[:max_idx]
right = arr[max_idx+1:]

sum_ = 0


if left :
    high = left[0][1]
    last_thing = left[0][0]
    for i in range(len(left)) :
        if high < left[i][1] :
            sum_ += (left[i][0] - last_thing) * high
            high = left[i][1] 
            last_thing = left[i][0]

    sum_ += ((arr[max_idx][0]-last_thing) * high)

if right : 
    high = right[-1][1]
    last_thing = right[-1][0]
    for i in range(len(right)-1,-1,-1) :
        if high <= right[i][1] :
            sum_ += (last_thing-right[i][0]) * high
            high = right[i][1] 
            last_thing = right[i][0]

    sum_ += ((last_thing - arr[max_idx][0]) * high)


sum_ += max_

print(sum_)