Updated:

1. 문제 링크

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

2. 사용 알고리즘

DP

3. 풀이

d[n] : a[n]을 마지막으로 하는 가장 큰 증가하는 부분 수열의 길이

d[n] = max(n 앞의 수 중, a[n]보다 작은 수의 가장 큰 증가하는 부분 수열의 길이) + a[n]

d[n] = max(d[i]) + a[n] (i < n, a[i] < a[n])

∴ max(d[i]) (1 <= i <= n)

4. 소스 코드

4-1. C++

4-1-1. Top-Down

https://github.com/dev-aiden/problem-solving/blob/main/boj/11055.cpp

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
#include <iostream>

using namespace std;

int d[1003], a[1003];

int solve(int n) {
    if (d[n] > 0) return d[n];
    d[n] = a[n];
    for (int i = n - 1; i >= 1; --i) {
        if (a[n] > a[i] && d[n] < solve(i) + a[n]) d[n] = d[i] + a[n];
    }
    return d[n];
}

int main(void) {
    ios_base::sync_with_stdio(false);
    int n; cin >> n;
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        solve(i);
    }
    d[1] = a[1];
    for (int i = 1; i <= n; ++i) if (ans < d[i]) ans = d[i];
    cout << ans << "\n";
    return 0;
}

4-1-2. Bottom-Up

https://github.com/dev-aiden/problem-solving/blob/main/boj/11055_2.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int d[1003], a[1003];

int main(void) {
    ios_base::sync_with_stdio(false);
    int n; cin >> n;
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i]; d[i] = a[i];
        for (int j = 1; j < i; ++j) {
            if (a[j] < a[i] && d[j] + a[i] > d[i]) d[i] = d[j] + a[i];
        }
        if (ans < d[i]) ans = d[i];
    }
    cout << ans << "\n";
    return 0;
}

4-2. JAVA

4-2-1. Top-Down

https://github.com/dev-aiden/problem-solving/blob/main/boj/11055.java

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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    static int d[] = new int[1003];
    static int a[] = new int[1003];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        int ans = 0;
        for (int i = 1; i <= n; ++i) {
            a[i] = Integer.parseInt(st.nextToken());
            solve(i);
        }
        d[1] = a[1];
        for (int i = 1; i <= n; ++i) if(ans < d[i]) ans = d[i];
        System.out.println(ans);
    }

    public static int solve(int n) {
        if (d[n] > 0) return d[n];
        d[n] = a[n];
        for (int i = n - 1; i >= 1; --i) {
            if (a[n] > a[i] && d[n] < solve(i) + a[n]) d[n] = d[i] + a[n];
        }
        return d[n];
    }
}

4-2-2. Bottom-Up

https://github.com/dev-aiden/problem-solving/blob/main/boj/11055_2.java

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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    static int d[] = new int[1003];
    static int a[] = new int[1003];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        int ans = 0;
        for(int i = 1; i <= n; ++i) {
            a[i] = Integer.parseInt(st.nextToken()); d[i] = a[i];
            for(int j = 1; j < i; ++j) {
                if (a[j] < a[i] && d[j] + a[i] > d[i]) d[i] = d[j] + a[i];
            }
            if (ans < d[i]) ans = d[i];
        }
        System.out.println(ans);
    }
}

Updated:

Leave a comment