1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 3


๐Ÿ“Œ ๋ฌธ์ œ

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


image


๐Ÿ“Œ ํ’€์ด

์™€โ€ฆ DP๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ๋Š๋‚€์ ์€ ์•„.. ๋ ๊ฑฐ๊ฐ™์€๋ฐ? ๊ฑฐ์˜ ํ’€์—ˆ๋Š”๋ฐ, ์•„ ์™œ์•ˆ๋˜์ง€โ€ฆ. ์ œ์ถœ: 3%์—์„œ ์˜ค๋‹ต ์ด๋Ÿฐ ์‹์ด์˜€๋‹ค.
์ง๊ด€์ ์œผ๋กœ ํ’€๋ ค๋‹ค๊ฐ€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉด์„œ ๊ฒ‰์žก์„ ์ˆ˜ ์—†์ด ์˜ค๋ฅ˜๊ฐ€ ์ปค์ง€๊ฒŒ ๋œ๋‹ค.

์ด ๋ฌธ์ œ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์ฒ˜์Œ์— class step๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ทธ ์•ˆ์— int current์™€ isSkip, totalStep์˜ ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด์„œ stepํ˜•์‹์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ , ์ „ ์ฃผ์†Œ๊ฐ’์˜ isSkip์ด true์ด๋ฉด -1, -2์˜ totalStep์„ ๋น„๊ตํ•˜์—ฌ ๋” ํฐ ๊ฐ’์— current๋ฅผ ๋”ํ•˜๊ณ , isSkip์ด false์ด๋ฉด -2์ฃผ์†Œ๊ฐ’์— current๊ฐ’์„ ๋”ํ•ด์ฃผ์—ˆ๋‹ค.
ํ•˜์ง€๋งŒ, ๋„์ €ํžˆ ์ด ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜๊ฐ€ ์—†์—ˆ๋‹ค.

์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐœ์„ ํ•˜๋ฉด์„œ, ์žฅ์ • 6์‹œ๊ฐ„๋™์•ˆ์ด๋‚˜ ๊ณ ๋ฏผ์„ ํ•˜์˜€๋Š”๋ฐ, ๋„์ €ํžˆ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€ ํ•˜๋‚˜๋„ ๊ฐ์ด ์žกํžˆ์ง€๊ฐ€ ์•Š์•˜๋‹ค.
๊ทธ๋ž˜์„œ ๋จผ์ € ํ‘ผ ์‚ฌ๋žŒ์—๊ฒŒ ๋ฌผ์–ด๋ณด์•˜๋‹ค.

์™€โ€ฆ ์„ค๋ช…์„ ๋“ฃ๋Š”๋ฐ ์ดํ•ด๊ฐ€ ์ „ํ˜€ ๋˜์ง€ ์•Š์•˜๋‹ค.
์•ž์—์„œ ์ง„์งœ ์—ด์‹ฌํžˆ ์„ค๋ช…์„ ํ•ด์ฃผ๋Š”๋ฐ, ๋จธ๋ฆฌ๋กœ๋Š” ์•„ ์ €๋ ‡๊ฒŒํ•˜๋ฉด ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋”ฐ๋ฅด๊ฒ ์ง€๋งŒ, ์–ด๋–ป๊ฒŒ ์ €๋Ÿฐ ์ƒ๊ฐ์„ ํ•˜์ง€โ€ฆ๋ผ๋Š” ์ƒ๊ฐ๋ฐ–์— ์•ˆ๋“ค์—ˆ๋‹ค.
๊ทธ ๋ฐฉ์‹์ด๋ž€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

image

๋‹ค์Œ๊ณผ ๊ฐ™์ด 0์˜ ์ฃผ์†Œ๋ฅผ ๋ฐŸ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

image

  • -3, -1์„ ๋ฐŸ๋Š” ๊ฒฝ์šฐ
    • dp[0] = dp[-3] + arr[-1] + arr[0]
  • -3, -2๋ฅผ ๋ฐŸ๋Š” ๊ฒฝ์šฐ
    • dp[0] = dp[-3] + arr[-2] + arr[0]
  • -2๋งŒ ๋ฐŸ๋Š” ๊ฒฝ์šฐ
    • dp[0] = dp[-2] + arr[0]

์ด๋•Œ dp์˜ ๊ฐ’์€ ํ•ญ์ƒ ์ตœ๋Œ€๊ฐ’์ธ๋ฐ, 2๋ฒˆ์งธ ๋ฐฉ๋ฒ•๊ณผ 3๋ฒˆ์จฐ ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋Š” ์‚ฌ์‹ค์ƒ ํ•ญ์ƒ 2๋ฒˆ์งธ๊ฐ€ ๋” ํฐ๊ฐ’์ด ๋œ๋‹ค.
๋”ฐ๋ผ์„œ 1๋ฒˆ, 2๋ฒˆ๊ฐ’์„ Math.max์„ ์ด์šฉํ•˜์—ฌ, ๋” dp[0]์— ์ €์žฅํ•˜๋ฉด, ๋ชจ๋“  dp๊ฐ’์€ ํ•ญ์ƒ ์ตœ๋Œ€๊ฐ€ ๋œ๋‹ค.

๊ณ„์† ๋…ธ๋ ฅํ•˜๋‹ค ๋ณด๋ฉด ์–ธ์  ๊ฐ€๋Š” ์ด ๋ฌธ์ œ๋ฅผ ๋ณด๋ฉด์„œ ์ด๋ ‡๊ฒŒ ์‰ฌ์šด๋ฐ, ์ด๊ฑธ ๊ณ ์ƒํ–ˆ๋‹ค๊ณ ? ํ•˜๋Š” ๋‚ ์ด ์˜ค์ง€ ์•Š์„๊นŒ... ๋ฐฑ์ค€์„ ํ’€๋ฉด์„œ ์š”์ฆ˜ ํ•ญ์ƒ ๋ถ€์กฑํ•˜๋‹ค๊ณ  ๋Š๋ผ๊ฒŒ ๋œ๋‹ค.


๐Ÿ“Œ Code

package DP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class N2579 {
    static int[] dp;
    static int[] arr;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int target = Integer.parseInt(st.nextToken());
        arr = new int[target];
        dp = new int[target];
        for (int i = 0; i < target; i++) {
            st = new StringTokenizer(br.readLine());
            arr[i] = Integer.parseInt(st.nextToken());
        }
        dp[0] = arr[0];
        if (target > 1) {
            dp[1] = arr[1] + arr[0];
        }
        if (target > 2) {
            dp[2] = Math.max(arr[0] + arr[2], arr[1] + arr[2]);
        }
        for (int i = 3; i < target; i++) {
            MakeMax(i);
        }

        System.out.println(dp[target - 1]);
    }

    public static void MakeMax(int index) {
        dp[index] = Math.max(dp[index - 3] + arr[index - 1], dp[index - 2]) + arr[index];
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜