728x90
1. 링크
https://www.acmicpc.net/problem/1092
2. 난이도(solved.ac 참고)
골드5
3. 풀이
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.acmicpc.net/problem/1092 | |
import sys | |
input = sys.stdin.readline | |
n = int(input()) | |
weight = sorted(list(map(int, input().split())), reverse=True) | |
m = int(input()) | |
box = sorted(list(map(int, input().split())), reverse=True) | |
time = 0 | |
if max(box) > max(weight): | |
print(-1) | |
else: | |
while len(box) > 0: | |
time += 1 | |
for i in range(len(weight)): | |
for j in range(len(box)): | |
if weight[i] >= box[j]: | |
box.pop(j) | |
break | |
print(time) |
가장 높은 중량을 들 수 있는 크레인에 가장 무거운 박스를 배치하면 된다. 크레인에 대한 반복문이 끝나면 1회 순회한 것이므로 while문 직후에 time이라는, 우리가 답으로 출력할 변수의 값을 1씩 증가해준다. 그리고 크레인의 값이 더 클 경우에 break을 함으로써 weight에 대해 불필요하게 남은 box에 대한 반복문을 돌 필요가 없게끔 하였다(break없이 돌면 안돼서 넣은 것이기도 함).
'CS > 알고리즘' 카테고리의 다른 글
[Python] BOJ(백준) 1149번 - RGB거리 (0) | 2021.10.09 |
---|---|
[Python] BOJ(백준) 2166번 - 다각형의 면적 (0) | 2021.10.09 |
[Python] BOJ(백준) 2170번 - 선 긋기(+ list와 tuple의 차이) (0) | 2021.10.02 |
[Python] BOJ(백준) 1850번 - 최대공약수 (0) | 2021.09.30 |
[Python] BOJ(백준) 17352번 - 여러분의 다리가 되어 드리겠습니다! (0) | 2021.09.29 |