UAIC Competitive Programming

Problem 36: Longest Increasing Subsequence

Problem Statement: Given an array of n integers, find the length of the longest strictly increasing subsequence.

Input

The first line contains an integer n. The second line contains n space-separated integers.

Output

Print the length of the longest increasing subsequence.

Examples


Input:
8
10 9 2 5 3 7 101 18

Output:
4
                

Solution Explanation

Method 1: Use binary search with a temporary list to maintain potential sequence ends and update it in O(n log n) time. Method 2: Use dynamic programming with O(n^2) complexity by comparing each element with every preceding element, which is simpler but slower.

Solution in C++


#include 
#include 
#include 
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n; cin >> n;
    vector a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    
    vector lis;
    for (int x : a) {
        auto it = lower_bound(lis.begin(), lis.end(), x);
        if(it == lis.end()) { 
            lis.push_back(x);
        } else {
            *it = x;
        }
    }
    cout << lis.size() << "
";
    return 0;
}