Algorithm/알고스팟
algospot - lis
jhg0406
2020. 2. 2. 15:53
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
42
43
44
45
46
47
|
//algospot - lis
#include <iostream>
#include <vector>
using namespace std;
int N;
vector<int> v;
vector<int> cache;
void init()
{
cin >> N;
v = vector<int>(N);
cache = vector<int>(N, -1);
for(int i = 0; i<N; ++i)
cin >> v[i];
}
int dp(int idx)
{
int& ret = cache[idx];
if(ret != -1)
return ret;
ret = 0;
for(int i = idx+1; i<N; ++i)
if(v[idx] < v[i])
ret = max(ret, dp(i));
++ret;
return ret;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int C; cin >> C;
for(int tn = 0; tn<C; ++tn)
{
init();
int ans = 0;
for(int i = 0; i<N; ++i)
ans = max(ans, dp(i));
cout << ans << "\n";
}
}
|
cs |
https://www.algospot.com/judge/problem/read/LIS
algospot.com :: LIS
Longest Increasing Sequence 문제 정보 문제 어떤 정수 수열에서 0개 이상의 숫자를 지우면 이 수열의 부분 수열 (subsequence) 를 얻을 수 있다. 예를 들어 10 7 4 9 의 부분 수열에는 7 4 9, 10 4, 10 9 등이 있다. 단, 10 4 7 은 원래 수열의 순서와 다르므로 10 7 4 9 의 부분 수열이 아니다. 어떤 부분 수열이 순증가할 때 이 부분 수열을 증가 부분 수열 (increasing subseque
www.algospot.com
lis
cache[idx] : idx를 처음으로 하는 수열의 최대 증가 부분 수열의 길이를 저장한다.
