Baekjoon

[#1965] 상자넣기

강람이 2020. 3. 16. 21:55
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
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
vector<int> boxes(10010);
vector<int> dp(10010);
 
int main() {
    int n;
    cin >> n;
 
    for (int i = 1; i <= n; i++)
        cin >> boxes[i];
 
    dp[1= 1;
    for (int i = 2; i <= n; i++) {
        int maxVal = -1;
        for (int j = 0; j <= i - 1; j++) {
            if (boxes[j] < boxes[i]) {
                maxVal = max(maxVal, dp[j]);
            }
        }
        dp[i] = maxVal + 1;
    }
 
    int result = -1;
    for (int i = 1; i <= n; i++) {
        result = max(result, dp[i]);
    }
    cout << result;
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

LIS 구하는 문제

주의해야할점은 dp 구하는 두번째 for문에서 j는 0부터 시작한다는 것

1부터 시작하면 5 1 의 경우 안됨

아니면 처음부터 dp를 다 1로 초기화를 해놓고 시작해서 1부터 시작하면 됨