nrk009

Lonsgest Increasing Subsequence in nlogn

Jan 30th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  5. #define ll long long
  6. #define ld long double
  7. #define pb push_back
  8. #define fe first
  9. #define se second
  10. #define nl "\n"
  11. #define pp pair < ll , ll >
  12. #define sz(x) (ll)x.size()
  13. #define st(x) sort(x.begin(),x.end())
  14. #define rst(x) sort(x.rbegin(), x.rend())
  15. #define all(x) x.begin(),x.end()
  16. long double pi = 3.14159265358979323;
  17.  
  18. const double EPS = 1e-12;
  19. const int N = 1e6 + 5;
  20. const int mod = 1e9 + 7;
  21.  
  22. //Day1
  23.  
  24. //Printing Longest Increasing Subsequence in nlogn
  25.  
  26. int main()
  27. {
  28.     fast;
  29.     ll t = 1;
  30.     //cin >> t;
  31.     while(t--){
  32.     ll n;
  33.     cin >> n ;
  34.     ll a[n];
  35.     for(int i = 0 ; i < n ;i++ ){
  36.         cin >> a[i];      
  37.     }
  38.     vector < pair < ll,  ll > > v;
  39.     v.pb({a[0],0});
  40.     vector < ll > prevind(n , -1);
  41.     ll h[n];
  42.     for(int i = 0 ; i < n ;i++ ){
  43.             h[i] = 1;
  44.     }
  45.     //memset(h , 0 , sizeof(h));
  46.     for(int i = 1 ; i < n ;i++ ){
  47.        // cout << a[i] << " ";
  48.         pair < ll , ll > p = {a[i] , -1};
  49.         ll j = lower_bound(v.begin() , v.end() ,p) - v.begin();
  50.         //cout << j << "\n";
  51.         if(j == 0){
  52.             v[j] = {a[i] ,i};
  53.         }
  54.         else if(j == sz(v)){
  55.             prevind[i] = v[j - 1].se;
  56.             v.pb({a[i],i});
  57.             h[i] += h[v[j-1].se];
  58.         }
  59.         else{
  60.             prevind[i] = v[j - 1].se;
  61.             v[j] = {a[i] , i};
  62.             h[i] += h[v[j-1].se];
  63.         }
  64.        // cout << h[i] << "\n";
  65.     }
  66.  
  67.     ll maxm = 0 ;
  68.     ll ind = 0 ;
  69.     for(int i = 0 ; i < n ;i++ ){
  70.             if(maxm < h[i]){
  71.                 maxm = h[i];
  72.                 ind = i;
  73.             }
  74.     }
  75.     cout << maxm << "\n";
  76.    /* cout << a[ind] << " ";
  77.     while(prevind[ind] != -1){
  78.         ind = prevind[ind];
  79.         cout << a[ind] << " ";
  80.     }
  81.     cout << "\n";*/
  82.     }
  83.    
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.    
  106.     return 0;
  107.    
  108. }
Add Comment
Please, Sign In to add comment