๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜ PS] ๋ฐฑ์ค€ 2579 ๊ณ„๋‹จ ์˜ค๋ฅด๊ธฐ ์ž๋ฐ” ๋ฌธ์ œ ํ’€์ด

by Wonit 2021. 1. 29.

๋ฌธ์ œ

ํ•ด๋‹น ํฌ์ŠคํŒ…์€ ๋ฐฑ์ค€์˜ 2547๋ฒˆ ๊ณ„๋‹จ ์˜ค๋ฅด๊ธฐ ์˜ ์ ‘๊ทผ๊ณผ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.
์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๋จผ์ € ์ƒ๊ฐํ•ด๋ณด์ž.

ํ•ด๊ฒฐ๋ฒ•

์ด ๋ฌธ์ œ๋Š” ํฌ๋„์ฃผ ์‹œ์‹ ๋ฌธ์ œ์™€ ์ œ๋ฒ• ์œ ์‚ฌํ•œ ๋ฌธ์ œ์ด๋‹ค.

 

ํ•˜์ง€๋งŒ ์ •๋‹ต ๋น„์œจ์ด 35 %๋‚˜ ๋˜๋Š”๋ฐ, ๊ทธ๊ฑด ๊ฐ„๊ณผํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์ด ํ•˜๋‚˜ ์žˆ์–ด์„œ ์ด๋‹ค.


์ž์„ธํ•œ ๊ฒƒ์€ ์•„๋ž˜์—์„œ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค.

์ ‘๊ทผ๋ฒ•

์—ฌ๊ธฐ์„œ ์ถœ๋ ฅ์ธ ๊ณ„๋‹จ ์ ์ˆ˜์˜ ์ตœ๋Œ€๊ฐ’์€ ๋ฌด์กฐ๊ฑด ๋งˆ์ง€๋ง‰ ๊ณ„๋‹จ์—์„œ ๋ฐœ์ƒํ•ด์•ผ ํ•œ๋‹ค.

 

๊ทธ๋Ÿผ ๋งˆ์ง€๋ง‰ ๊ณ„๋‹จ์€ ๊ผญ ๋ฐŸ์•„์•ผ ํ•œ๋‹ค๋Š” ์†Œ๋ฆฌ์ด๋‹ค.


๊ทธ๋Ÿผ ๋งˆ์ง€๋ง‰ ๊ณ„๋‹จ์„ ๊ผญ ๋ฐŸ์„ ๊ฒฝ์šฐ๋Š” ๋ช‡๊ฐ€์ง€ ์ผ๊นŒ?

 

n ๊ฐœ์˜ ๊ณ„๋‹จ์ด ์žˆ์„ ๋•Œ, ๋งˆ์ง€๋ง‰ ๊ณ„๋‹จ์„ ๊ผญ ๋ฐŸ์„ ๊ฒฝ์šฐ 2๊ฐ€์ง€๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž.

  1. n-1๋ฒˆ์งธ ๊ณ„๋‹จ์„ ๋ฐŸ๊ณ  ์˜ค๋Š” ๊ฒฝ์šฐ
  2. n-2๋ฒˆ์งธ ๊ณ„๋‹จ์„ ๋ฐŸ๊ณ  ์˜ค๋Š” ๊ฒฝ์šฐ

 

n-1๋ฒˆ์งธ ๊ณ„๋‹จ์„ ๋ฐŸ๊ณ  ์˜ค๋Š” ๊ฒฝ์šฐ

์ด ๊ฒฝ์šฐ๋Š” n-2๋ฒˆ์งธ ๊ณ„๋‹จ์„ ๊ฑด๋„ˆ ๋›ฐ๊ณ  ์˜ค๋Š” ๊ฒฝ์šฐ์—ฌ์•ผ ํ•œ๋‹ค.


๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 3๊ฐœ์˜ ๊ณ„๋‹จ์„ ์—ฐ์†์ ์œผ๋กœ ๋ฐŸ๊ณ  ์˜ค๋Š” ๊ฒƒ์ด ๋˜๋ฏ€๋กœ n-3, n-1, n ์ด ๋˜์–ด์•ผ ํ•œ๋‹ค.

๋งˆ์ง€๋ง‰ ๊ณ„๋‹จ + ์ „์ „ ๊ณ„๋‹จ์˜ ์ตœ๋Œ€๊ฐ’

 

n-2๋ฒˆ์งธ ๊ณ„๋‹จ์„ ๋ฐŸ๊ณ  ์˜ค๋Š” ๊ฒฝ์šฐ

์ด ๊ฒฝ์šฐ๋Š” n-1๋ฒˆ์งธ ๊ณ„๋‹จ์„ ๋ฐŸ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์ด๋‹ค.


๊ทธ๋Ÿผ n-2๋ฒˆ์งธ ๊ณ„๋‹จ์—์„œ ๋ฐ”๋กœ n๋ฒˆ ๊ณ„๋‹จ์œผ๋กœ ๊ฐ€๋Š” ๊ฒฝ์šฐ์ธ ๊ฒƒ์ด๋‹ค.

์ „์ „ ๊ณ„๋‹จ์˜ ์ตœ๋Œ€๊ฐ’

์ด ๋‘ ๊ฒฝ์šฐ์˜ ์ตœ๋Œ€๋ฅผ ํ˜„์žฌ ๋ฐŸ๊ณ  ์žˆ๋Š” ๊ณ„๋‹จ์˜ ์ตœ๋Œ€๋กœ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

์˜ค๋‹ต ํ›„๋ณด

ํ•ด๋‹น ๋ฌธ์ œ์˜ ์ •๋‹ต๋ฅ ์ด 35%์ธ ๊ฐ€์žฅ ํฐ ์ด์œ ๋Š” ๋ฐ”๋กœ ์ฒซ ๋ฒˆ์งธ ๊ณ„๋‹จ์ด๋‹ค.


๋ฌธ์ œ์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ๊ณ„๋‹จ์„ ๋ฌด์กฐ๊ฑด ๋ฐŸ์œผ๋ผ๋Š” ์ด์•ผ๊ธฐ๊ฐ€ ์—†๋‹ค.


ํ•˜์ง€๋งŒ ์ฒซ ๋ฒˆ์งธ ๊ณ„๋‹จ์„ ๋ฐŸ์œผ๋ฉด ๋ฌด์กฐ๊ฑด ์ด๋“์ฒ˜๋Ÿผ ๋Š๊ปด์ง„๋‹ค.


์™œ? ์‹œ์ž‘์ ์€ ๊ณ„๋‹จ์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ–ˆ์œผ๋‹ˆ๊นŒ.


๊ทผ๋ฐ ์ค‘์š”ํ•œ๊ฑด ์‹œ์ž‘์ ์ด ๊ณ„๋‹จ์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์ด์ง€ ์ฒซ ๋ฒˆ์งธ ๊ณ„๋‹จ์„ ์นด์šดํŒ…ํ•˜์ง€ ์•Š๊ฒ ๋‹ค๋Š” ์†Œ๋ฆฌ๋Š” ์•„๋‹ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ๊ฒฐ๊ตญ ์ฒซ ๋ฒˆ์งธ ๊ณ„๋‹จ์„ ๋ฐŸ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ด๋“์ผ ์ˆ˜ ์žˆ๋‹ค.

 

arr = { 1,   100,   200,   300} ๋งŒ ๋ด๋„ arr = { 1,   100,   200,   300} ์ธ๊ฒŒ ํ™•์‹คํžˆ ์ด๋“์ด๋‹ค

์ •๋‹ต ์ฝ”๋“œ

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int n = Integer.parseInt(br.readLine());
        int[] arr = new int[301];
        int[] dp = new int[301];
        for (int i = 0; i < n; i++) {
            arr[i] = Integer.parseInt(br.readLine());
        }

        dp[0] = arr[0];
        dp[1] = Math.max(arr[1], arr[0] + arr[1]);
        dp[2] = Math.max(arr[1] + arr[2], arr[0] + arr[2]);

        for (int i = 3; i < n; i++) {
            dp[i] = Math.max(dp[i-3] + arr[i-1], dp[i-2]) + arr[i];
        }

        bw.write(String.valueOf(dp[n-1]));
        bw.flush();
        bw.close();
    }
}

๋ฌธ์ œ ํšŒ๊ณ 

์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๋ฌด์กฐ๊ฑด ์ฒซ ๋ฒˆ์งธ ๊ณ„๋‹จ์„ ๋ฐŸ๋Š”๊ฒŒ ์ด๋“์ด๊ฒ ์ง€ ํ•˜๋Š” ๋ถ€์กฑํ•œ ์‚ฌ๊ณ ๋กœ 5๋ฒˆ์ด๋‚˜ ํ‹€๋ ธ์—ˆ๋‹ค.
์ ํ™”์‹ ๋„์ถœ ์ž์ฒด๋Š” ์–ด๋ ต์ง€ ์•Š์œผ๋‚˜ ์™œ ์ •๋‹ต๋ฅ ์ด ๋‚ฎ์€์ง€ ์•Œ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ

์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋Œ“๊ธ€