๋ฐฑ์ค€/C++

[๋ฐฑ์ค€/C++] 25214๋ฒˆ ํฌ๋ฆผ ํŒŒ์Šคํƒ€

yulee_to 2022. 7. 1. 19:21

๋ฐฑ์ค€ ๋กœ๊ณ 
๋ฐฑ์ค€
25214๋ฒˆ ํฌ๋ฆผํŒŒ์Šคํƒ€
25214๋ฒˆ ํฌ๋ฆผํŒŒ์Šคํƒ€

๐Ÿ’ก์ฒซ๋ฒˆ์งธ ์•„์ด๋””์–ด

์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์„ ๋”ฐ๋กœ ์ €์žฅํ•ด์„œ ์ถ”๊ฐ€๋œ ๊ฐ’์ด ์ตœ๋Œ“๊ฐ’๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ์ตœ์†Ÿ๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•ด์ฃผ๋Š” ๋ฐฉ์‹์„ ์ƒ๊ฐํ–ˆ๋‹ค.

์ด ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” 1 <= i <= j <= |A| ๋ผ๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ์ˆ˜ ์—†์—ˆ๋‹ค.

 

 

๐Ÿ’ก๋‘๋ฒˆ์งธ ์•„์ด๋””์–ด

์ตœ์†Ÿ๊ฐ’์„ ๋”ฐ๋กœ ์ €์žฅํ•˜๋Š” ๊ฑด ์ฒซ๋ฒˆ์งธ ์•„์ด๋””์–ด์™€ ๋™์ผํ•˜๋‹ค. ๋‹ค๋งŒ ์ตœ๋Œ“๊ฐ’์„ ๋”ฐ๋กœ ์ €์žฅํ•˜์ง€ ์•Š๊ณ  (์ถ”๊ฐ€๋œ ๊ฐ’ - ์ตœ์†Ÿ๊ฐ’)๊ณผ dp๋ฐฐ์—ด์˜ ์ด์ „ ๊ฐ’์„ ๋น„๊ตํ•ด์„œ ๋” ํฐ ๊ฒƒ์„ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด๋ƒˆ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด ์˜ˆ์ œ 1๋ฒˆ์˜ ๊ฒฝ์šฐ
์ถ”๊ฐ€ ํšŸ์ˆ˜(i) ์ถ”๊ฐ€ํ•œ ๊ฐ’ ์ตœ์†Ÿ๊ฐ’ ์ถ”๊ฐ€-์ตœ์†Œ dp[i-1] dp[i]
1 50 50 0 dp[0] = 0์œผ๋กœ
์ดˆ๊ธฐํ™”
0
2 100 50 50 0 50
3 70 50 20 50 50
4 110 50 60 50 60
5 10 10 0 60 60
6 100 10 90 60 90
์œ„ ํ‘œ์ฒ˜๋Ÿผ (์ถ”๊ฐ€-์ตœ์†Œ)๊ณผ dp[i-1] ์ค‘ ๋” ํฐ ๊ฐ’์ด dp[i]๊ฐ€ ๋œ๋‹ค.

 

์ฝ”๋“œ๋Š” 1ํŠธ๋งŒ์— ์„ฑ๊ณต!

25214๋ฒˆ ํฌ๋ฆผํŒŒ์Šคํƒ€
๋งž์•˜์Šต๋‹ˆ๋‹ค!!

 

์ œ์ถœํ•œ ์ฝ”๋“œ

#include <iostream>
using namespace std;

int dp[200000];
int min_num;

int main (){
    cin.tie(NULL);
    ios::sync_with_stdio(false);

    int t, x;
    cin >> t;
    for(int i=0; i < t; i++){
        cin >> x;
        if( i==0 ) {
            dp[0] = 0;
            min_num = x;}
        else {
            if ( x < min_num ) { min_num = x;}
            dp[i] = (x-min_num) > dp[i-1] ? (x-min_num) : dp[i-1];
        }
        cout << dp[i] << " " ;

    }
}
728x90