Skip to content

Commit ca00622

Browse files
authored
Add files via upload
1 parent ca63c40 commit ca00622

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
11.6 KB
Binary file not shown.

code(Increasing Subsequence).cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
3+
#include <bits/stdc++.h>
4+
#define ll long long
5+
using namespace std;
6+
7+
int LongestIncreasingSubsequenceLength(vector<ll int>& v)
8+
{
9+
if (v.size() == 0)
10+
return 0;
11+
12+
vector<ll int> tail(v.size(), 0);
13+
ll int length = 1;
14+
15+
tail[0] = v[0];
16+
17+
for (ll int i = 1; i < v.size(); i++)
18+
{
19+
20+
// binary search
21+
22+
auto b = tail.begin(), e = tail.begin() + length;
23+
auto it = lower_bound(b, e, v[i]);
24+
25+
26+
if (it == tail.begin() + length)
27+
tail[length++] = v[i];
28+
else
29+
*it = v[i];
30+
}
31+
32+
return length;
33+
}
34+
35+
int main()
36+
{
37+
38+
ios_base::sync_with_stdio(false);
39+
cin.tie(NULL);
40+
cout.tie(NULL);
41+
42+
ll int n;
43+
cin>>n;
44+
45+
vector<ll int> v(n);
46+
for(ll int&i : v)
47+
cin>>i;
48+
49+
50+
cout << LongestIncreasingSubsequenceLength(v) <<endl;
51+
52+
return 0;
53+
}

0 commit comments

Comments
 (0)