[SWEA] 5201. [파이썬 S/W 문제해결 구현] 3일차 - 컨테이너 운반 [D3]

yunh·2022년 3월 29일
0

알고리즘 - SWEA 🐣

목록 보기
79/103
post-thumbnail

📚 문제

https://swexpertacademy.com/main/learn/course/subjectDetail.do?courseId=AVuPDYSqAAbw5UW6&subjectId=AWUYEGw61n8DFAVT


📖 풀이

컨테이너와 트럭을 내림차순으로 정렬한다.

트럭이 큰 순서로 확인한다. 컨테이너도 큰 순서로 확인하며 담을 수 있으면 무게에 더해준다.

📒 코드

for tc in range(1, int(input()) + 1):
    n, m = map(int, input().split())
    containers = list(map(int, input().split()))
    trucks = list(map(int, input().split()))
    containers.sort(reverse=True)
    trucks.sort(reverse=True)
    c, t = 0, 0     # 컨테이너와 트럭을 큰 것부터 확인
    total = 0       # 총 무게

    while t < m and c < n:  # 컨테이너나 트럭이 인덱스를 초과하면 종료
        if trucks[t] >= containers[c]:  # 담을 수 있는 경우
            total += containers[c]
            t += 1                      # 다음 트럭을 담는다.
        c += 1                          # 컨테이너는 항상 다음으로

    print(f'#{tc} {total}')

🔍 결과

profile
passionate developer

0개의 댓글