카테고리 없음

백준 1092 배

hw.kr 2022. 12. 24. 20:48

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

 

1092번: 배

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

www.acmicpc.net

📌 해결과정

주어진 시간 1에 가능한 많은 크레인을 사용할 수 있도록 해야, 시간을 최소로 사용할 수 있다.

 

📌 코드

 1. 3중 반복문

import sys

input = sys.stdin.readline

n = int(input())
crane_list = list(map(int, input().split()))
m = int(input())
box_list = list(map(int, input().split()))

crane_list.sort(reverse=True)
box_list.sort(reverse=True)


if box_list[0] > crane_list[0]:
    print(-1)
else:
    time = 0
    while box_list:

        for crane in crane_list:
            for box in box_list:
                if crane >= box:
                    box_list.remove(box)
                    break

        time += 1

    print(time)

 

2. crane_next, box_moved 배열 추가 

import sys

input = sys.stdin.readline

n = int(input())
crane = list(map(int, input().split()))
m = int(input())
box = list(map(int, input().split()))

crane.sort(reverse=True)
box.sort(reverse=True)


box_moved = [0] * m
crane_next = [0] * n

move_count = 0
time = 0

if box[0] > crane[0]:
    print(-1)
else:
    while move_count < m:
        for i in range(n):
            while crane_next[i] < m:
                if not box_moved[crane_next[i]] and crane[i] >= box[crane_next[i]]:
                    box_moved[crane_next[i]] = 1
                    crane_next[i] += 1
                    move_count += 1
                    break

                crane_next[i] += 1

        time += 1

    print(time)