728x90

 0. [c++] 백준


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


 1. 풀이


이번 문제에서 입력N의 크기는 1000으로 N^2의 연산이여도 충분히 해결이 가능한 문제이다.

따라서 모든 경우의 수를 구할 수 있는 동적계획법을 활용하여 문제를 해결하였다.


추가적으로 메모이제이션(cache부분)을 활용하여 한번 방문을 한 이후에는 추가적인 계산을 하지 않도록 구현해주었다.



 2. 소스코드


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
40
41
#include<iostream>
#include<algorithm>
#include<vector>
 
 
using namespace std;
 
typedef unsigned long long ull;
 
int cache[1001];
int arr[1001];
int N;
 
int dp(int here) {
    int& ret = cache[here];
    if (ret != 0)
        return ret;
    ret = 1;
    for (int next = here + 1; next < N; next++) {
        if (arr[here] < arr[next])
            ret = max(ret, dp(next) + 1);
    }
 
    return ret;
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    cin >> N;
    int ret = 0;
    for (int i = 0; i < N; i++) {
        cin >> arr[i];
    }
    for (int i = 0; i < N; i++) {
        ret = max(ret, dp(i));
    }
    cout << ret;
    return 0;
}
cs


 3. 참고





질문이나 지적 있으시면 댓글로 남겨주세요~

도움 되셨으면 하트 꾹!


+ Recent posts