Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIS.cpp #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions dynamic_programming_problems/LIS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+5;
//const int mod = 998244353;
const int mod = 1e9+7;
#define pb push_back
#define mp make_pair
#define sz size
#define fi first
#define se second
#define nl "\n"
#define ps(x) fixed<<setprecision(x)
#define all(x) (x).begin(), (x).end()
int gcd(int a, int b) { return b?gcd(b,a%b):a; }
//*****************************************************************************************//
int LIS(vector<int>& arr, int n) {
if(n<=1) return n;
vector<int>dp;
dp.push_back(arr[0]);
for(int i=1; i<n; i++){
if(dp.back()<arr[i])
dp.push_back(arr[i]);
else{
int j=lower_bound(dp.begin(), dp.end(), arr[i])-dp.begin();
dp[j]=arr[i];
}
}
return dp.size();
}
void code(){
int n; cin>>n;
vector<int>arr(n);
for(int i=0; i<n; i++){
cin>>arr[i];
}
int res=LIS(arr, n);
cout<<res<<nl;
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif

int T=1; //cin>>T;
for(int i=1; i<=T; i++) code();

#ifndef ONLINE_JUDGE
cerr<<"Actual : "<<1.0*clock()/CLOCKS_PER_SEC<<" sec\n";
#endif
return 0;
}