olrlobt

[์ž๋ฐ”/๋ฐฑ์ค€ ๊ณจ๋“œ4] #1715 ์นด๋“œ ์ •๋ ฌํ•˜๊ธฐ ๋ณธ๋ฌธ

Algorithm/๋ฐฑ์ค€

[์ž๋ฐ”/๋ฐฑ์ค€ ๊ณจ๋“œ4] #1715 ์นด๋“œ ์ •๋ ฌํ•˜๊ธฐ

olrlobt 2022. 12. 31. 02:59

๐Ÿ”’ ๊ณจ๋“œ4 - #1715 ์นด๋“œ ์ •๋ ฌํ•˜๊ธฐ

 

๐Ÿ“Œ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ถ”๊ฐ€ ํžŒํŠธ

5
10
20
10
10
40

= 190
4
10
10
10
30

= 110

 

 

โœ๏ธ ํ’€์ด๋ฒ•

 

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์ดํ•ดํ–ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋„ ํ—ท๊ฐˆ๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•˜๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

 

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์„ธ์šฐ๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ, ๋”ํ•œ ๊ฐ’๋“ค์„ ํ•œ ๋ฒˆ์”ฉ ์“ฐ๋ฉด์„œ ๋‹ค ํ•ฉ์นœ ์ดํ›„.

์“ด ๊ฐ’๋“ค์„ ๋‹ค ๋”ํ•ด์ฃผ์–ด์•ผ ๋‹ต์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค๋Š” ์ ์„ ์•Œ๊ณ  ์ง„ํ–‰ํ•˜๋ฉด, ์ดํ•ดํ•˜๊ธฐ ํŽธํ•  ๊ฒƒ ๊ฐ™๋‹ค.

 

์ฒซ ๋ฒˆ์งธ ์ผ€์ด์Šค๋ฅผ ์˜ˆ๋กœ ๋“ค์ž๋ฉด, 90์ด ๋‚˜์˜ค๋Š” ๊ณผ์ •์—์„œ ๋ชจ๋“  ์นด๋“œ๊ฐ€ ํ•ฉ์ณ์ง€๊ฒŒ ๋œ๋‹ค.

์ด ํ›„, ํŒŒ๋ž€ ๊ฐ’ ( ๋”ํ•œ ๊ณผ์ •์—์„œ ๋‚˜์˜จ ๊ฐ’) ๋“ค์„ ๋‹ค ํ•ฉ์น˜๋ฉด ์›ํ•˜๋Š” ์ •๋‹ต 190 ์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

 

 

 

 

์ด ๋ฌธ์ œ๋Š” ํ† ๋„ˆ๋จผํŠธ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ คํ•˜๊ฑฐ๋‚˜, ๋ฆฌ๊ทธ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์•ˆ ๋œ๋‹ค.

์™œ๋ƒํ•˜๋ฉด, ๊ฐ€์žฅ ์ž‘์€ ์นด๋“œ ๋ญ‰์น˜๋ถ€ํ„ฐ ๋ญ‰์ณ์ฃผ์–ด์•ผ, ์ตœ์†Ÿ๊ฐ’์ด ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

๋”ฐ๋ผ์„œ ์นด๋“œ ๋ญ‰์น˜๋ฅผ ๋ญ‰์น˜๊ณ , ๋‹ค์‹œ ๋น„๊ตํ•ด ์ฃผ๋Š” ์ž‘์—…์„ ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

 

 

์˜ˆ๋ฅผ๋“ค์–ด, ๋‘๋ฒˆ์งธ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋ณด์ž.

๋‹จ์ˆœํ•˜๊ฒŒ ํ† ๋„ˆ๋จผํŠธ ๋ฐฉ์‹์œผ๋กœ ์ƒ๊ฐํ•˜๊ฒŒ๋˜๋ฉด, ์ •๋‹ต์€ 110์ธ๋ฐ,  120์ด ๋‚˜์˜ค๊ฒŒ ๋˜๋ฏ€๋กœ ์˜ค๋‹ต์ด๋‹ค.

 

 

๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ 110์ด ๋‚˜์˜ค๊ฒŒ ํ•  ์ˆ˜ ์žˆ์„๊นŒ?

 

 

๋ฐ”๋กœ

ํ† ๋„ˆ๋จผํŠธ๊ฐ€ ์•„๋‹Œ, ํ•œ ๋ญ‰์น˜๋ฅผ ํ•ฉ์น˜๊ณ  ์ฒ˜์Œ๋ถ€ํ„ฐ ์ƒˆ๋กœ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์ฒซ๋ฒˆ์งธ ๋น„๊ต๊ฐ€ ๋๋‚œ ํ›„, ๋‚จ์€ ์นด๋“œ ๋”๋ฏธ๋Š” [ 20, 10, 30 ] ์ด๋‹ค. ํ† ๋„ˆ๋จผํŠธ ๋Œ€๋กœ๋ผ๋ฉด 10๊ณผ 30 ์„ ํ•ฉ์ณค์ง€๋งŒ,

์šฐ๋ฆฌ๋Š” ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ, 10๊ณผ 20์„ ํ•ฉ์น˜๋Š” ๊ฒƒ์ด ๋งž๋‹ค.

 

 

๋”ฐ๋ผ์„œ, ํ•œ ๋ฒˆ ํ•ฉ์น˜๋Š” ์ž‘์—… ์ดํ›„์— ์ƒˆ๋กœ ์ •๋ ฌํ•˜๊ณ  ๋‹ค์‹œ ๋น„๊ตํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค.

 

๋‚˜๋Š” ์ฒ˜์Œ์— ํ† ๋„ˆ๋จผํŠธ๋กœ ํ’€๋ ค๊ณ  ํ–ˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— Queue๋ฅผ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค. ์•ˆ ๋œ๋‹ค๋Š” ๊ฑธ ๊นจ๋‹ฌ์€ ์ดํ›„, ์ •๋ ฌ์„ ์ž๋™์œผ๋กœ ํ•ด ์ฃผ๊ธฐ ์œ„ํ•˜์—ฌ ์šฐ์„ ์ˆœ์œ„ ํ (PriorityQueue)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

 

 

 

ํ’€์ด ๊ณผ์ •

    1. ์ž‘์€ ์ˆœ ์ •๋ ฌ

    2. ๊ฐ€์žฅ ์ž‘์€ ๋‘ ๋ฑ์„ ํ•ฉ์นœ๋‹ค.

    3. ์œ„ ๊ณผ์ •์„ ๋ฑ์˜ ์ˆ˜๊ฐ€ 1์ด ๋ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

 

๐Ÿ—๏ธ ํ’€์ด

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class baekjoon1715 {

    private static int sum = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        List<Integer> decks = new ArrayList<>();

        for (int time = 0; time < N; time++) {
            decks.add(sc.nextInt());
        }

        solve(decks);
        System.out.println(sum);
    }

    public static void solve(List<Integer> decks) {
        PriorityQueue<Integer> queDeck = new PriorityQueue<>(decks);
        sumDecks(queDeck);
    }


    private static void sumDecks(PriorityQueue<Integer> decks) {
        if (decks.size() == 1) {
            return;
        }

        int sumTwin = decks.poll() + decks.poll();
        sum += sumTwin;
        decks.add(sumTwin);

        sumDecks(decks);
    }
}
Comments