Advertisement
fooker

longest_increasing_subsequence

May 7th, 2023 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. ll max_ll(ll x, ll y){
  5.     return ((x>y)?(x):(y));
  6. }
  7. int main()
  8. {
  9.     ll n;
  10.     cin>>n;
  11.     ll a[n+1];
  12.     for (ll i=1; i<=n; i++) cin>>a[i];
  13.     ll dp[n+1];
  14.     multiset<pair<ll,ll>> s;
  15.     s.insert({a[1],1});
  16.     dp[1]=1;
  17.     for (ll i=2; i<=n; i++){
  18.         s.insert({a[i],i});
  19.         dp[i]=1;
  20.         for (auto it=s.find({a[i],i}); it!=s.begin(); it--){
  21.             if ((*it).first<a[i]) dp[i]=max_ll(dp[i],dp[(*it).second]+1);
  22.         }
  23.         auto it=s.begin();
  24.         if ((*it).first<a[i]) dp[i]=max_ll(dp[i],dp[(*it).second]+1);
  25.     }
  26.     for (ll i=1; i<=n; i++) dp[n]=max_ll(dp[n],dp[i]);
  27.     cout<<dp[n];
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement