0412 Greedy

누디·2023년 4월 12일
0

알고리즘

목록 보기
5/5

11399 : ATM

n = int(input())
times = list(map(int, input().split()))

time = 0

times.sort()

for i in range(len(times)):
    for j in range(i+1):
        time += times[j]

print(time)

이중 for문 쓰면 바로 풀리는 문제!
백준에서 풀었던 것 중에 제일 쉬웠다

2457 : 공주님의 정원

# 꽃이 피는 기간이 가장 긴 꽃들 순으로 배열
import sys

n = int(sys.stdin.readline())
flower = []

for _ in range(n):
    start_m, start_d, end_m, end_d = map(int, sys.stdin.readline().split())
    flower.append([start_m * 100 + start_d, end_m * 100 + end_d])

flower.sort()

end_date = 301
cnt = 0

while (flower):
    if end_date >= 1201 or flower[0][0] > end_date:
        break
    
    tmp_end_date = -1
    
    for _ in range(len(flower)):
        if flower[0][0] <= end_date:
            if tmp_end_date <= flower[0][1]:
                tmp_end_date = flower[0][1]

            flower.remove(flower[0])
        
        else:
            break
    end_date = tmp_end_date
    cnt += 1

if end_date < 1201:
    print(0)
else:
    print(cnt)

어제 풀었던 회의실 배정 문제와 비슷했지만, 날짜를 다룬다는 점에서 골드가 된 것이 아닐까 생각함!
내 생각에 중요한 것은 마지막과 리스트의 요소를 하나하나 비교한 후 그 다음 처리하는 방법..?

처음 input() 썼더니 시간초과 떴다 ㅠㅠ
sys 함수를 사용하자..!

한달 후엔 골드를 풀고 있길... 제발!

1541 : 잃어버린 괄호

equation = input().split('-')
s = 0

for i in equation[0].split('+'):
    s += int(i)
for i in equation[1:]:
    for j in i.split('+'):
        s -= int(j)

print(s)

마이너스(-)를 기준으로 자르는 것이 핵심이었던 문제

0개의 댓글