๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][BAEKJOON][DP] 9456๋ฒˆ ์Šคํ‹ฐ์ปค

ibelieveinme 2021. 8. 22. 17:04
728x90

๋ฌธ์ œ

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

 

9465๋ฒˆ: ์Šคํ‹ฐ์ปค

์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ์งธ ์ค„์—๋Š” n (1 ≤ n ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ๋‘ ์ค„์—๋Š” n๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ, ๊ฐ ์ •์ˆ˜๋Š” ๊ทธ ์œ„์น˜์— ํ•ด๋‹นํ•˜๋Š” ์Šคํ‹ฐ์ปค์˜

www.acmicpc.net

 

๋ฌธ์ œ์„ค๋ช…

 

์ฝ”๋“œ

#include <iostream>
#include <algorithm>
using namespace std;

int sticker[2][100001];

int searchMaxScore(int);

int main() {
    int testcase = 0;
    cin >> testcase;

    for (int t = 0; t < testcase; t++) {
        int column = 0;
        cin >> column;

        for (int row = 0; row < 2; row++) {
            for (int col = 1; col <= column; col++) {//๋ฐ˜๋ณต๋ฌธ์„ index 2๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด
                cin >> sticker[row][col];
            }
        }
        cout << searchMaxScore(column) << "\n";
    }
}

int searchMaxScore(int column) {
    for (int i = 2; i <= column; i++) {
        //์œ„ ์Šคํ‹ฐ์ปค์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜
        sticker[0][i] += max(sticker[1][i - 1], sticker[1][i - 2]);
        //์•„๋ž˜ ์Šคํ‹ฐ์ปค์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜
        sticker[1][i] += max(sticker[0][i - 1], sticker[0][i - 2]);
    }
    return max(sticker[0][column], sticker[1][column]);
}
728x90