Updated:

1. 문제 링크

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

2. 사용 알고리즘

DP

3. 풀이

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

d[n] = max(n 앞의 수 중, a[n]보다 큰 수의 가장 긴 감소하는 부분 수열의 길이) + 1

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

∴ 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/11722.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] = 1;
    for (int i = n - 1; i >= 1; --i) {
        if (a[n] < a[i] && d[n] < solve(i) + 1) d[n] = d[i] + 1;
    }
    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] = 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/11722_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 a[1003], d[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] = 1;
        for (int j = 1; j < i; ++j) {
            if (a[i] < a[j] && d[i] < d[j] + 1) d[i] = d[j] + 1;
        }
        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/11722.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] = 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] = 1;
        for (int i = n - 1; i >= 1; --i) {
            if (a[n] < a[i] && d[n] < solve(i) + 1) d[n] = d[i] + 1;
        }
        return d[n];
    }
}

4-2-2. Bottom-Up

https://github.com/dev-aiden/problem-solving/blob/main/boj/11722_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] = 1;
            for(int j = 1; j < i; ++j) {
                if (a[i] < a[j] && d[i] < d[j] + 1) d[i] = d[j] + 1;
            }
            if (ans < d[i]) ans = d[i];
        }
        System.out.println(ans);
    }
}

Updated:

Leave a comment