Coding Test/BaekJoon_Python

백준 1026 <보물> Python

JunOnJuly 2023. 11. 8. 22:35
728x90

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

 

1026번: 보물

첫째 줄에 N이 주어진다. 둘째 줄에는 A에 있는 N개의 수가 순서대로 주어지고, 셋째 줄에는 B에 있는 수가 순서대로 주어진다. N은 50보다 작거나 같은 자연수이고, A와 B의 각 원소는 100보다 작거

www.acmicpc.net


B 를 정렬하면 안된다는 조건이 까다로웠다. A를 B로 정렬하는 방법을 찾는다면 풀 수 있다.


def solution(N, A, B):
    # 리스트 정렬
    A = sorted(A)
    # A를 정렬한 인덱스 리스트와 와 B 를 병합
    A_index_list = list(range(N))
    A_index_list = zip(A_index_list, B)
    # B 에 따라 A_index_list 를 정렬
    A_index_list = sorted(A_index_list, key=lambda x: -x[1])
    # 분리
    A_index_list = [a[0] for a in A_index_list]
    # A 를 A_index_list 를 참고해서 정렬
    temp_A = [0 for _ in range(N)]
    for idx in range(N):
        temp_A[A_index_list[idx]] = A[idx]
    # 곱해서 더해줌
    min_sum = 0
    for a, b in zip(temp_A, B):
        min_sum += a*b

    return min_sum


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

print(solution(N, A, B))

 

728x90

'Coding Test > BaekJoon_Python' 카테고리의 다른 글

백준 2230 <수 고르기> Python  (0) 2023.11.09
백준 2217 <로프> Python  (0) 2023.11.08
백준 1931 <회의실 배정> Python  (0) 2023.11.07
백준 11047 <동전 0> Python  (0) 2023.11.07
백준 10844 <쉬운 계단 수> Python  (0) 2023.11.05