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

[BOJ][C++] ๋ฐฑ์ค€ 2313๋ฒˆ: ๋ณด์„ ๊ตฌ๋งคํ•˜๊ธฐ

์„ ๋‹ฌ 2024. 9. 23. 18:40
๋ฐ˜์‘ํ˜•

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

 

๋ฌธ์ œ

๋ณด์„ ๊ฐ€๊ฒŒ์— ์—ฌ๋Ÿฌ ๊ฐ€์ง€์˜ ๋ณด์„์ด ์ง„์—ด๋˜์–ด ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋ณด์„์€ ์ •์ˆ˜๋กœ ํ‘œํ˜„๋˜๋Š” ๊ฐ€์น˜๊ฐ€ ์žˆ๋‹ค. ๋•Œ๋กœ๋Š” ์ €์ฃผ๋ฐ›์€ ๋ณด์„์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์น˜๊ฐ€ ์Œ์ˆ˜๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๋‹ค.

๋ณด์„๋“ค์€ ์ด n๊ฐœ์˜ ์ค„์— ๋‚˜์—ด๋˜์–ด ์žˆ๋‹ค. ์ด์ œ ๋‹น์‹ ์€ ๊ฐ๊ฐ์˜ ์ค„์—์„œ ๋ช‡ ๊ฐœ์˜ ๋ณด์„์„ ๊ตฌ๋งคํ•˜๋ ค ํ•œ๋‹ค. ์ด๋•Œ, ๊ฐ ์ค„์—์„œ ๋ณด์„์„ ๊ตฌ๋งคํ•  ๋•Œ ์—ฐ์†์ ์ธ ๋ณด์„๋“ค์„ ๊ตฌ๋งคํ•ด์•ผ ํ•œ๋‹ค. ์ฆ‰, ์–ด๋Š ํ•œ ์ค„์—์„œ 1, 2๋ฒˆ ๋ณด์„์„ ๊ตฌ๋งคํ•  ์ˆ˜๋„ ์žˆ๊ณ , 2, 3๋ฒˆ ๋ณด์„์„ ๊ตฌ๋งคํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, 1, 3๋ฒˆ ๋ณด์„์„ ๊ตฌ๋งคํ•  ์ˆ˜๋Š” ์—†๋‹ค.

๊ตฌ๋งคํ•˜๋Š” ๋ณด์„์˜ ๊ฐ€์น˜์˜ ์ด ํ•ฉ์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋„๋ก ๋ณด์„์„ ๊ตฌ๋งคํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ˆ˜ n(1 ≤ n ≤ 1,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ 2×n๊ฐœ์˜ ์ค„์—๋Š” n๊ฐœ์˜ ์ค„์— ๋‚˜์—ด๋œ ๋ณด์„๋“ค์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋จผ์ € ๊ฐ ์ค„์— ๋‚˜์—ด๋œ ๋ณด์„์˜ ๊ฐœ์ˆ˜ L(1 ≤ L ≤ 1,000)์ด ์ฃผ์–ด์ง€๊ณ , ๊ทธ ๋‹ค์Œ ์ค„์— L๊ฐœ์˜ ์ •์ˆ˜๋“ค์ด ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ์ •์ˆ˜๋Š” ๊ฐ ๋ณด์„์˜ ๊ฐ€์น˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋ณด์„์˜ ๊ฐ€์น˜๋Š” ์ ˆ๋Œ“๊ฐ’์ด 10,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜์ด๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋ณด์„์˜ ๊ฐ€์น˜์˜ ์ด ํ•ฉ์˜ ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋‹ค์Œ n๊ฐœ์˜ ์ค„์—๋Š”, ์ค„์—์„œ ๋ช‡ ๋ฒˆ์งธ ๋ณด์„๋ถ€ํ„ฐ ๋ช‡ ๋ฒˆ์งธ ๋ณด์„๊นŒ์ง€๋ฅผ ๊ตฌ๋งคํ–ˆ๋Š”์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๋งŒ์•ฝ ์ตœ๋Œ€๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์—ฌ๋Ÿฟ์ด๋ฉด, ๊ตฌ๋งคํ•œ ๋ณด์„๋“ค์˜ ์ด ๊ฐœ์ˆ˜๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ๋ฐฉ๋ฒ•์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ด์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋„ ์—ฌ๋Ÿฟ์ด๋ผ๋ฉด, ์ถœ๋ ฅํ•œ n×2๊ฐœ์˜ ์ˆ˜๋“ค์„ ํ•˜๋‚˜์˜ ์ˆ˜์—ด๋กœ ์ƒ๊ฐํ•˜์—ฌ, ์‚ฌ์ „์‹์œผ๋กœ ๊ฐ€์žฅ ์•ž์— ์˜ค๋Š” ๊ฒฝ์šฐ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

ํ’€์ด

์ž…๋ ฅ ์กฐ๊ฑด์„ ๋ณด๋ฉด ๋ณด์„ ๊ฐ€์น˜์˜ ์ด ํ•ฉ์€

์ตœ๋Œ€ n*L*(๋ณด์„์˜ ๊ฐ€์น˜ ์ตœ๋Œ“๊ฐ’) = 1000*1000*10000 = 10,000,000,000 = 100์–ต

23์–ต์„ ํ•œ์ฐธ ๋„˜๊ธฐ๋ฏ€๋กœ int๊ฐ€ ์•„๋‹Œ long long์œผ๋กœ ํ•ด์•ผํ•œ๋‹ค

 

๋ณด์„ ๊ฐ ์ค„๋งˆ๋‹ค dp ์—ฐ์‚ฐ์€ ๋…๋ฆฝ์ ์œผ๋กœ ์ง„ํ–‰ํ•˜์ง€๋งŒ 

์ถœ๋ ฅ ์ฒซ ์ค„์— ์ตœ์ข…์œผ๋กœ ๋‚˜์˜จ ๊ฐ’์„ ๋ณด์—ฌ์ค˜์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—

์ž…๋ ฅ์„ ์ „๋ถ€ ๋‹ค ๋ฐ›์•„์„œ ์ €์žฅํ•ด๋†“๊ณ  ์‹œ์ž‘ํ•ด์•ผํ•œ๋‹ค. (ํ•„์ž๋Š” v๋ผ๋Š” ๋ฒกํ„ฐ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค)

 

dp[i] : ๋ณด์„๊ฐฏ์ˆ˜, i๋ฒˆ์งธ๊นŒ์ง€์˜ ์ตœ๋Œ€ํ•ฉ

        // dp
        vector<ci>dp(length); // dp[i] : ๋ณด์„๊ฐฏ์ˆ˜, i๋ฒˆ์งธ๊นŒ์ง€์˜ ์ตœ๋Œ€ํ•ฉ
        dp[0] = {1, v[i][0]};
        for(int j=1; j<length; j++) {
            ci before = dp[j-1];
            if(before.second<=0) {
                dp[j] = {1, v[i][j]};
            } else {
                dp[j] = {before.first+1, before.second+v[i][j]};
            }
        }

 

 

๋น„๊ต ์กฐ๊ฑด์ด ๊นŒ๋‹ค๋กญ๋‹ค.

1. ๋ณด์„์˜ ๊ฐ€์น˜์˜ ํ•ฉ์ด ์ตœ๋Œ€๊ฐ€ ๋˜์–ด์•ผํ•จ

2. ๊ฐ€์น˜์˜ ํ•ฉ์ด ๊ฐ™๋‹ค๋ฉด -> ๋ณด์„์˜ ๊ฐฏ์ˆ˜๊ฐ€ ์ ์–ด์•ผํ•จ

3. ๊ฐ€์น˜๋„ ๊ฐ™๊ณ  ๊ฐฏ์ˆ˜๋„ ๊ฐ™๋‹ค๋ฉด -> ๊ทธ๋‚˜๋งˆ ์•ž์ชฝ์ธ๊ฑฐ

// ์ตœ๋Œ€๊ฐ€ ๋˜๋Š” ์ฒซ๋ฒˆ์งธ ์œ„์น˜ ๊ตฌํ•˜๊ธฐ
        int index = 0;
        int cnt = dp[index].first;
        int max_value = dp[index].second;
        for(int j=1; j<length; j++) {
            if(dp[j].second>=max_value) {
                if(dp[j].second==max_value && dp[j].first>=cnt) {
                    continue;
                }
                index = j;
                max_value = dp[j].second;
                cnt = dp[j].first;
            }
        }

 

๊ฒŒ์‹œํŒ์— ์นœ์ ˆํ•œ ๋ฐ˜๋ก€ ๋ชจ์Œ์ด ์žˆ๋‹ค.

์•„๋ž˜ ๊ธ€์— ๋‚˜์™€์žˆ๋Š” ๋ฐ˜๋ก€๋ฅผ ๋‹ค ์„ฑ๊ณตํ•˜๋ฉด ๊ฑฐ์˜ ์„ฑ๊ณต์ด๋‹ค

https://www.acmicpc.net/board/view/139927

 

 

์ฝ”๋“œ

// ํ’€์ด : https://whkakrkr.tistory.com

#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> ci;

int main() {
	// ์ž…๋ ฅ
	int n;
	cin >> n;
	vector<vector<int>>v(n);
    for(int i=0; i<n; i++) {
        int tmp;
        cin >> tmp;
        v[i] = vector<int>(tmp);
        for(int j=0; j<tmp; j++) {
            cin >> v[i][j];
        }
    }
    
    // ํ’€์ด
    ll sum = 0;
    vector<ci> ans(n);
    for(int i=0; i<n; i++) {
        int length = v[i].size();
        
        // dp
        vector<ci>dp(length); // dp[i] : ๋ณด์„๊ฐฏ์ˆ˜, i๋ฒˆ์งธ๊นŒ์ง€์˜ ์ตœ๋Œ€ํ•ฉ
        dp[0] = {1, v[i][0]};
        for(int j=1; j<length; j++) {
            ci before = dp[j-1];
            if(before.second<=0) {
                dp[j] = {1, v[i][j]};
            } else {
                dp[j] = {before.first+1, before.second+v[i][j]};
            }
        }
        
        // ์ตœ๋Œ€๊ฐ€ ๋˜๋Š” ์ฒซ๋ฒˆ์งธ ์œ„์น˜ ๊ตฌํ•˜๊ธฐ
        int index = 0;
        int cnt = dp[index].first;
        int max_value = dp[index].second;
        for(int j=1; j<length; j++) {
            if(dp[j].second>=max_value) {
                if(dp[j].second==max_value && dp[j].first>=cnt) {
                    continue;
                }
                index = j;
                max_value = dp[j].second;
                cnt = dp[j].first;
            }
        }
        sum += max_value;
        ans[i] = {index+1-cnt+1, index+1};
    }
    
    // ์ถœ๋ ฅ
    cout << sum << "\n";
    for(ci i : ans) {
        cout << i.first << " " << i.second << "\n";
    }
    
    return 0;
}

 

๋ฐ˜์‘ํ˜•