본문 바로가기
Problem Solving/Baekjoon

[백준] 9329 패스트 푸드 상금 - Greedy / Java

by graycode 2024. 3. 11.

 문제 링크

 

9329번: 패스트 푸드 상금

입력은 여러개의 테스트 케이스로 이루어져있다. 각 테스트 케이스마다 첫째 줄에는 서로 다른 상금의 종류 n (1 ≤ n ≤ 10) 과 코치가 가지고 있는 스티커의 종류 (1 ≤ m ≤ 30, 종류는 1부터 m까지

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuilder sb = new StringBuilder();

        int t = read();
        while (t-- > 0) {
            int n = read(), m = read();
            Price[] prices = new Price[n];

            for (int i = 0; i < n; i++) {
                int k = read();
                List<Integer> list = new ArrayList<>();

                while (k-- > 0) list.add(read());
                prices[i] = new Price(read(), list);
            }

            int[] cnt = new int[m + 1];
            for (int i = 1; i <= m; i++) cnt[i] = read();

            int sum = 0;
            label:
            for (Price price : prices) {
                int min = Integer.MAX_VALUE;
                for (int i : price.list) {
                    if (cnt[i] < 1) continue label;
                    min = Math.min(min, cnt[i]);
                }

                sum += price.price * min;
            }

            sb.append(sum).append("\n");
        }

        bw.write(sb.toString());
        bw.flush();
    }

    private static class Price {
        int price;
        List<Integer> list;

        Price(int price, List<Integer> list) {
            this.price = price;
            this.list = list;
        }
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글