본문 바로가기
알고리즘

Python - 배 (1092) 그리디

by jun.s.gi 2023. 6. 17.
728x90

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

 

1092번: 배

첫째 줄에 N이 주어진다. N은 50보다 작거나 같은 자연수이다. 둘째 줄에는 각 크레인의 무게 제한이 주어진다. 이 값은 1,000,000보다 작거나 같다. 셋째 줄에는 박스의 수 M이 주어진다. M은 10,000보

www.acmicpc.net

 

import sys
n = int(sys.stdin.readline().strip())
crain = list(map(int, sys.stdin.readline().strip().split()))
boxNum = int(sys.stdin.readline().strip())
weight = list(map(int, sys.stdin.readline().strip().split()))

crain.sort()
weight.sort()

if crain[-1] < weight[-1]:
    print(-1)
    exit()

ans = 0
cnt = 0
crainIdx = 0
boxIdx = 0
while cnt != boxNum:
    ans += 1
    crainIdx = n-1
    
    for box in range(len(weight)-1, -1, -1):
        if crainIdx == -1: break
        if weight[box] == 0:continue
        if crain[crainIdx] >= weight[box]:
            cnt += 1
            crainIdx -= 1
            del weight[box]
print(ans)
728x90