๐Ÿ•๏ธ ICPC Sinchon/Dynamic Programming

[BOJ S1][C++] ๋ฐฑ์ค€ 2156๋ฒˆ: ํฌ๋„์ฃผ ์‹œ์‹

์„ ๋‹ฌ 2022. 9. 30. 04:52
๋ฐ˜์‘ํ˜•

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

 

2156๋ฒˆ: ํฌ๋„์ฃผ ์‹œ์‹

ํšจ์ฃผ๋Š” ํฌ๋„์ฃผ ์‹œ์‹ํšŒ์— ๊ฐ”๋‹ค. ๊ทธ ๊ณณ์— ๊ฐ”๋”๋‹ˆ, ํ…Œ์ด๋ธ” ์œ„์— ๋‹ค์–‘ํ•œ ํฌ๋„์ฃผ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ํฌ๋„์ฃผ ์ž”์ด ์ผ๋ ฌ๋กœ ๋†“์—ฌ ์žˆ์—ˆ๋‹ค. ํšจ์ฃผ๋Š” ํฌ๋„์ฃผ ์‹œ์‹์„ ํ•˜๋ ค๊ณ  ํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‘ ๊ฐ€์ง€ ๊ทœ

www.acmicpc.net

 

๋ฌธ์ œ

ํšจ์ฃผ๋Š” ํฌ๋„์ฃผ ์‹œ์‹ํšŒ์— ๊ฐ”๋‹ค. ๊ทธ ๊ณณ์— ๊ฐ”๋”๋‹ˆ, ํ…Œ์ด๋ธ” ์œ„์— ๋‹ค์–‘ํ•œ ํฌ๋„์ฃผ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ํฌ๋„์ฃผ ์ž”์ด ์ผ๋ ฌ๋กœ ๋†“์—ฌ ์žˆ์—ˆ๋‹ค. ํšจ์ฃผ๋Š” ํฌ๋„์ฃผ ์‹œ์‹์„ ํ•˜๋ ค๊ณ  ํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‘ ๊ฐ€์ง€ ๊ทœ์น™์ด ์žˆ๋‹ค.

  1. ํฌ๋„์ฃผ ์ž”์„ ์„ ํƒํ•˜๋ฉด ๊ทธ ์ž”์— ๋“ค์–ด์žˆ๋Š” ํฌ๋„์ฃผ๋Š” ๋ชจ๋‘ ๋งˆ์…”์•ผ ํ•˜๊ณ , ๋งˆ์‹  ํ›„์—๋Š” ์›๋ž˜ ์œ„์น˜์— ๋‹ค์‹œ ๋†“์•„์•ผ ํ•œ๋‹ค.
  2. ์—ฐ์†์œผ๋กœ ๋†“์—ฌ ์žˆ๋Š” 3์ž”์„ ๋ชจ๋‘ ๋งˆ์‹ค ์ˆ˜๋Š” ์—†๋‹ค.

ํšจ์ฃผ๋Š” ๋  ์ˆ˜ ์žˆ๋Š” ๋Œ€๋กœ ๋งŽ์€ ์–‘์˜ ํฌ๋„์ฃผ๋ฅผ ๋ง›๋ณด๊ธฐ ์œ„ํ•ด์„œ ์–ด๋–ค ํฌ๋„์ฃผ ์ž”์„ ์„ ํƒํ•ด์•ผ ํ• ์ง€ ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ๋‹ค. 1๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™์–ด ์žˆ๋Š” n๊ฐœ์˜ ํฌ๋„์ฃผ ์ž”์ด ์ˆœ์„œ๋Œ€๋กœ ํ…Œ์ด๋ธ” ์œ„์— ๋†“์—ฌ ์žˆ๊ณ , ๊ฐ ํฌ๋„์ฃผ ์ž”์— ๋“ค์–ด์žˆ๋Š” ํฌ๋„์ฃผ์˜ ์–‘์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ํšจ์ฃผ๋ฅผ ๋„์™€ ๊ฐ€์žฅ ๋งŽ์€ ์–‘์˜ ํฌ๋„์ฃผ๋ฅผ ๋งˆ์‹ค ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. 

์˜ˆ๋ฅผ ๋“ค์–ด 6๊ฐœ์˜ ํฌ๋„์ฃผ ์ž”์ด ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ์ž”์— ์ˆœ์„œ๋Œ€๋กœ 6, 10, 13, 9, 8, 1 ๋งŒํผ์˜ ํฌ๋„์ฃผ๊ฐ€ ๋“ค์–ด ์žˆ์„ ๋•Œ, ์ฒซ ๋ฒˆ์งธ, ๋‘ ๋ฒˆ์งธ, ๋„ค ๋ฒˆ์งธ, ๋‹ค์„ฏ ๋ฒˆ์งธ ํฌ๋„์ฃผ ์ž”์„ ์„ ํƒํ•˜๋ฉด ์ด ํฌ๋„์ฃผ ์–‘์ด 33์œผ๋กœ ์ตœ๋Œ€๋กœ ๋งˆ์‹ค ์ˆ˜ ์žˆ๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ํฌ๋„์ฃผ ์ž”์˜ ๊ฐœ์ˆ˜ n์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ n ≤ 10,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ n+1๋ฒˆ์งธ ์ค„๊นŒ์ง€ ํฌ๋„์ฃผ ์ž”์— ๋“ค์–ด์žˆ๋Š” ํฌ๋„์ฃผ์˜ ์–‘์ด ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. ํฌ๋„์ฃผ์˜ ์–‘์€ 1,000 ์ดํ•˜์˜ ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜์ด๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ตœ๋Œ€๋กœ ๋งˆ์‹ค ์ˆ˜ ์žˆ๋Š” ํฌ๋„์ฃผ์˜ ์–‘์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

ํ’€์ด

[๐ŸŒฒ Altu-Bitu/0927 ๋™์ ๊ณ„ํš๋ฒ•] - [BOJ][C++] ๋ฐฑ์ค€ 2579๋ฒˆ: ๊ณ„๋‹จ ์˜ค๋ฅด๊ธฐ

์œ„ ๋ฌธ์ œ์™€ ๊ฑฐ์˜ ๋™์ผํ•œ๋ฐ, ๋งˆ์ง€๋ง‰๊ณ„๋‹จ์„ ๊ผญ ๋ฐŸ์•„์•ผํ•œ๋‹ค๋Š” ์กฐ๊ฑด๋งŒ ์ œ์™ธํ•œ๋‹ค.

๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆด ๋•Œ ํ˜„์žฌ ํฌ๋„์ฃผ๋ฅผ ๋งˆ์‹  ๋ฒ„์ „๊ณผ ๋งˆ์‹œ์ง€ ์•Š์€ ๋ฒ„์ „ ๋‘๊ฐ€์ง€๋ฅผ ๋น„๊ตํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผํ•œ๋‹ค.

dp[i] = max(dp[i-1], dp[i]);

 

// Authored by : seondal
// Co-authored by : -

// #include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <deque>

using namespace std;

int maxWine(int n, vector<int>&wine) {
    vector<int> dp(n,0);
    
    dp[0] = wine[0];
    dp[1] = wine[0] + wine[1];
    
    for(int i=2; i<n; i++) {
        dp[i] = max(dp[i-2], dp[i-3]+wine[i-1]) + wine[i];
        dp[i] = max(dp[i-1], dp[i]);
    }
    
    return dp[n-1];
}

int main() {
    int n;
    cin >> n;
    vector<int> wine(n,0);
    for(int i=0; i<n; i++)
        cin >> wine[i];
    
    cout << maxWine(n, wine);
    
    return 0;
}

/*
 */

 

๋ฐ˜์‘ํ˜•