[백준 9465] 스티커

2024. 8. 30. 03:10·Study/Algorithm
728x90

문제


접근

  • DP 사용해서 이전의 결과값을 사용해서 효율적인 계산을 진행하였다.
  • 예시
    • 스티커를 뗀 부분이 O이고
    • 찢겨진 부분은 X라고 하자.

dp 배열과 스티커 배열의 사이즈

  • (0,2) 위치의 스티커를 뗀 경우는 아래와 같이 나누어진다. 지금 목표는 스티커를 뗀 경우 중 가장 큰 값을 얻었을 때이기 때문에 이때 둘 중에 큰 값을 선택하고 (0,2) 위치의 스티커 값을 더해주면 된다.
    • 이외에 아무것도 떼지 않은 경우
    • (1,1) 위치의 스티커를 뗀 경우

 

  • (0,3) 위치의 스티커를 뗀 경우는 아래와 같이 나누어진다. 
    • (1,2) 위치의 스티커를 뗀 경우
    • (1,1) 위치의 스티커를 뗀 경우
    • 해당 값 중에 큰 값을 선택하고 (0,3) 위치의 스티커 값을 더해주면 된다.

 

그러면 아래와 같은 점화식을 얻을 수 있다.

구한 점화식을 코드에 적용하면 아래 코드와 같다.


코드

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

public class Main {
    static int testCase;
    static int N;
    static int[][] sticker;
    static int[][] dp;
    static BufferedReader br;
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        testCase = Integer.parseInt(br.readLine());
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < testCase; i++) {
            N = Integer.parseInt(br.readLine());
            sticker = new int[2][N+1];
            settingScore(N);
            dp = new int[2][N+1];
            dp[0][1] = sticker[0][1];
            dp[1][1] = sticker[1][1];

            for (int j = 2; j <= N; j++) {
                dp[0][j] = Math.max(dp[1][j - 1], dp[1][j - 2]) + sticker[0][j];
                dp[1][j] = Math.max(dp[0][j - 1], dp[0][j - 2]) + sticker[1][j];
            }

            int maxScore = Math.max(dp[0][N], dp[1][N]);
            sb.append(maxScore + "\n");
        }
        System.out.println(sb.toString());
    }

    private static void settingScore(int n) throws IOException {
        for (int i = 0; i < 2; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int j = 1; j < n + 1; j++) {
                sticker[i][j] = Integer.parseInt(st.nextToken());
            }
        }
    }

}
728x90
반응형
저작자표시 (새창열림)

'Study > Algorithm' 카테고리의 다른 글

[백준 2638] 치즈 (골드 3)  (0) 2025.05.13
[백준 5525] IOIOI  (1) 2024.09.03
[백준 1987] 알파벳  (0) 2024.08.30
[백준 11726] 2*n 타일링  (2) 2024.08.30
[백준 11725] 트리의 부모 찾기  (1) 2024.08.30
'Study/Algorithm' 카테고리의 다른 글
  • [백준 2638] 치즈 (골드 3)
  • [백준 5525] IOIOI
  • [백준 1987] 알파벳
  • [백준 11726] 2*n 타일링
pink_salt
pink_salt
유익함을 주는 개발자가 되도록 keep going
  • pink_salt
    KeepGoingForever
    pink_salt
  • 전체
    오늘
    어제
    • 분류 전체보기 (117)
      • Project (7)
      • WEB study (3)
        • WEB(Springboot) (10)
        • Git, GitLab (13)
        • Clean code (1)
        • FrontEnd (3)
      • Study (21)
        • Algorithm (19)
        • 면접 준비 (2)
      • Cloud Computing (2)
        • AWS (2)
      • 프로그래밍 언어 (35)
        • Java (29)
        • Python (0)
        • javascript (6)
      • 운영체제 (0)
        • Linux (0)
      • Database (4)
        • MongoDB (8)
        • SQL (8)
      • 애플리케이션 개발 (1)
        • Android (1)
      • AI (1)
        • Deeplearning (1)
        • machinelearning (0)
      • Daily (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    spring boot
    Query
    무료IT교육
    gitlab
    mysql
    IT교육
    티스토리챌린지
    빅오표기법
    자바
    개념
    Java
    SWEA
    언어
    오블완
    코딩강의
    SW
    코드프레소
    Database
    무료코딩교육
    Git
    코딩이러닝
    백준
    BFS
    dp
    대외활동
    git branch
    객체지향
    MongoDB
    codepresso
    python
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
pink_salt
[백준 9465] 스티커
상단으로

티스토리툴바