-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlongest_increasing_sequence.cpp
More file actions
41 lines (33 loc) · 1.14 KB
/
Copy pathlongest_increasing_sequence.cpp
File metadata and controls
41 lines (33 loc) · 1.14 KB
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
//
// LIS (Longest Increasing Sequence)
// 数列 a の最長増加部分列を求める
// is_strong = true のとき狭義単調増加なもの、false のとき広義単調増加なもの
//
// verified
// AOJ Course DPL_1_D Combinatorial - Longest Increasing Subsequence
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_D&lang=jp
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// dp[i] := 長さが i の増加部分列として最後尾の要素のとりうる最小値
template<class T> int LIS(vector<T> a, bool is_strong = true) {
const T INF = 1<<30; // to be set appropriately
int n = (int)a.size();
vector<T> dp(n, INF);
for (int i = 0; i < n; ++i) {
if (is_strong) *lower_bound(dp.begin(), dp.end(), a[i]) = a[i];
else *upper_bound(dp.begin(), dp.end(), a[i]) = a[i];
}
return lower_bound(dp.begin(), dp.end(), INF) - dp.begin();
}
//------------------------------//
// Examples
//------------------------------//
int main() {
int N; cin >> N;
vector<int> a(N);
for (int i = 0; i < N; ++i) cin >> a[i];
cout << LIS(a) << endl;
}