Advertisement
Josif_tepe

Untitled

Oct 15th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace  std;
  4. int n;
  5. int niza[105];
  6. int dp[105];
  7. int LIS(int i) {
  8.     if(dp[i] != -1) {
  9.         return dp[i];
  10.     }
  11.     int longest_sequence = 0;
  12.     for(int j = i + 1; j < n; j++) {
  13.         if(niza[i] <= niza[j]) {
  14.             longest_sequence = max(longest_sequence, LIS(j) + 1);
  15.         }
  16.     }
  17.    
  18.     return dp[i] = longest_sequence;
  19. }
  20. int main() {
  21.  
  22.     cin >> n;
  23.    
  24.     for(int i = 0; i < n; i++) {
  25.         cin >> niza[i];
  26.         dp[i] = -1; // znaci deka nemame nisto presmetano vo dinamickoto programiranje
  27.     }
  28.     int longest_sequence = 0;
  29.     for(int i = 0; i < n; i++) {
  30.         longest_sequence = max(longest_sequence, LIS(i) + 1);
  31.     }
  32.     cout << longest_sequence << endl;
  33. }
  34. // 4 3 1 3 5 6 2 10 11
  35. // LIS(0) = max(LIS(4) + 1, LIS(5) + 1, LIS(7) + 1, LIS(8) + 1) = 4
  36. // LIS(4) = max(LIS(5) + 1, LIS(7) + 1, LIS(8) + 1) = max(3, 2, 1) = 3
  37. // LIS(5) = max(LIS(7) + 1, LIS(8) + 1) = max(2, 1) = 2
  38. // LIS(7) = max(LIS(8) + 1) = 0 + 1 = 1
  39. // LIS(8) = 0
  40.  
  41.  
  42. // LIS(i)
  43.  
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement