Josif_tepe

Untitled

Dec 23rd, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. const int maxn = 2502;
  5.  
  6. int n;
  7. int a[maxn];
  8.  
  9. int dp[maxn];
  10.  
  11. int rec(int at) {
  12.    
  13.     if(dp[at] != -1) {
  14.         return dp[at];
  15.     }
  16.     int res = 1;
  17.    
  18.    
  19.     for(int i = at + 1; i < n; i++) {
  20.         if(a[at] < a[i]) {
  21.             res = max(res, rec(i) + 1);
  22.         }
  23.     }
  24.    
  25.     dp[at] = res;
  26.     return res;
  27. }
  28. int main() {
  29.    
  30.     memset(dp, -1, sizeof dp);
  31.     cin >> n;
  32.    
  33.     for(int i = 0; i < n; i++) {
  34.         cin >> a[i];
  35.     }
  36.    
  37.     int res = 0;
  38.     for(int i = 0; i < n; i++) {
  39.         res = max(res, rec(i));
  40.     }
  41.    
  42.     cout << res << endl;
  43.     return 0;
  44. }
  45. // rec(0) = rec(6) + 1 = 2
  46. // rec(0) = rec(7) + 1 = 2
  47. // rec(6) = 1
  48. // rec(7) = 1
  49.  
Advertisement
Add Comment
Please, Sign In to add comment