C++ Day 39 STL Containers (Deep Understanding & Real Usage) Till now, you already know arrays, vectors, loops, and STL algorithms. Today, we go one step deeper and understand STL containers , which are the backbone of modern C++ programming. In real projects and competitive coding, choice of container matters a lot. 1. What are STL Containers? STL containers are data structures provided by C++ to store data efficiently. They handle: memory management resizing element access performance optimization You focus on logic , not memory handling. 2. Categories of STL Containers STL containers are mainly divided into: Sequence Containers Associative Containers Unordered Containers Container Adapters 3. Sequence Containers These store data in sequence . 3.1 Vector Most used container in C++. vector< int > v; Key Features: Dynamic size Contiguous memory Fast random access Slower insertion in middle Example: v. push_...
You are given an array containing
integers. Your task is to determine the longest increasing subsequence in the array, i.e., the longest subsequence where every element is larger than the previous one.
A subsequence is a sequence that can be derived from the array by deleting some elements without changing the order of the remaining elements.
Input
The first line contains an integer : the size of the array.
After this there are integers : the contents of the array.
Output
Print the length of the longest increasing subsequence.
Constraints
Example
Input:
8 7 3 5 3 6 2 9 8
Output:
4
#include <bits/stdc++.h>using namespace std;void solve(){int n;cin>>n;vector<int> arr(n);for(int i=0;i<n;i++){cin>>arr[i];}vector<int> lis;for(int i=0;i<n;i++){if(lis.empty()||lis.back()<arr[i]){lis.push_back(arr[i]);}else{auto it=lower_bound(lis.begin(),lis.end(),arr[i]);*it=arr[i];}}cout<<lis.size()<<endl;}int main() {solve();}
Comments
Post a Comment