본문 바로가기

백준/DP

[KOI] 백준 10835 - 카드게임

728x90

www.acmicpc.net/problem/10835

 

다음 가지치기로 얻을 수 있는 최대 점수를 동적계획법으로 구하면 된다.

 

왼쪽 카드를 버렸을 때 / 양쪽 카드를 버렸을 때 / 오른쪽 카드를 버릴 수 있을 경우 버리고 점수를 얻었을 때

 

전체 코드

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <bits/stdc++.h>
 
using namespace std;
 
int n;
int l[2000], r[2000];
int dp[2000][2000];
 
int memo(int ldx, int rdx);
 
int main()
{
    memset(dp, -1sizeof dp);
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> n;
 
    for (int i = 0; i < n; ++i)
        cin >> l[i];
    for (int i = 0; i < n; ++i)
        cin >> r[i];
    
    cout << memo(00);
}
 
int memo(int ldx, int rdx)
{
    if (ldx == n || rdx == n)
        return 0;
    
    int &ret = dp[ldx][rdx];
 
    if (ret != -1)
        return ret;
    
    ret = max(memo(ldx + 1, rdx + 1), memo(ldx + 1, rdx));
    if (l[ldx] > r[rdx])
        ret = max(ret, r[rdx] + memo(ldx, rdx + 1));
    return ret;
}
cs

728x90

'백준 > DP' 카테고리의 다른 글

백준 2399 - 거리의 합  (0) 2021.05.09
백준 2315 - 가로등 끄기  (0) 2021.03.12
백준 1311 - 할 일 정하기 1  (0) 2021.02.09
[USACO] 백준 14165 - Team Building  (0) 2021.01.31
[KOI] 백준 1866 - 택배  (0) 2021.01.28