๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

https://softeer.ai/practice/info.do?idx=1&eid=395&sw_prbl_sbms_sn=213446 

 

Softeer

์—ฐ์Šต๋ฌธ์ œ๋ฅผ ๋‹ด์„ Set์„ ์„ ํƒํ•ด์ฃผ์„ธ์š”. ์ทจ์†Œ ํ™•์ธ

softeer.ai


1. ๋ฌธ์ œ

๋ฐฐ๋‚ญ์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฌด๊ฒŒ W, ๊ท€๊ธˆ์† ์ข…๋ฅ˜ ๊ฐœ์ˆ˜ turn์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š”๋‹ค.

๋ฐฐ๋‚ญ์„ ์ฑ„์šธ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๋น„์‹ผ ๊ฐ€๊ฒฉ์„ ๊ตฌํ•ด ์ถœ๋ ฅํ•œ๋‹ค.

2. ํ’€์ด

๊ฐ„๋‹จํ•œ ๊ตฌํ˜„ ๋ฌธ์ œ์ด๋‹ค.

1. ๊ฐ€๊ฒฉ ๋†’์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” pq๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

2. pq๊ฐ€ ๋น„์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉฐ

- ๋‹ค์Œ ๊ท€๊ธˆ์†์˜ ๋ฌด๊ฒŒ๊ฐ€ ๋‚จ์€ ๋ฌด๊ฒŒ๋ณด๋‹ค ํด ๊ฒฝ์šฐ, ans ๊ฐฑ์‹ , ๋ฐ˜๋ณต๋ฌธ break;

- ๋‹ค์Œ ๊ท€๊ธˆ์†์˜ ๋ฌด๊ฒŒ๊ฐ€ ๋‚จ์€ ๋ฌด๊ฒŒ๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ, ans ๊ฐฑ์‹ , ๋‚จ์€ ๋ฌด๊ฒŒ ๊ฐฑ์‹ 

3. ์ฝ”๋“œ

import java.util.*;
import java.io.*;


public class Main
{
    static PriorityQueue<Node> pq = new PriorityQueue<> ((o1,o2)-> (o2.price-o1.price));
    static int W, turn, ans;

    public static void main(String args[]) throws Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        W = Integer.parseInt(st.nextToken());
        turn = Integer.parseInt(st.nextToken());

        for (int i=0; i<turn; i++) {
            st = new StringTokenizer(br.readLine());
            pq.offer(new Node(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())));
        }
        //์ž…๋ ฅ ๋

        solution();
        System.out.println(ans);
    }

    static void solution() {
        while (!pq.isEmpty()) {
            Node n = pq.poll();
            if (n.weight >= W) {
                ans += W * n.price;
                break;
            }
            else {
                ans += n.weight * n.price;
                W -= n.weight;
            }
        }
    }

    static class Node {
        int weight, price;
        Node(int weight, int price) {
            this.weight = weight;
            this.price = price;
        }
    }
}

4. ์‚ฌ๋‹ด

๊ฐˆ ๋•Œ๊นŒ์ง€ ํ•˜์ž ๋ˆ„๊ฐ€์ด๊ธฐ๋‚˜ ํ•ด๋ณด์ž