nrk009

LIS O(n^2)

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