Advertisement
Josif_tepe

Untitled

Jun 15th, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <vector>
  4. using namespace std;
  5. const int maxn = 100005;
  6.  
  7. int n;
  8. int A[maxn], B[maxn];
  9. int dp[maxn][2];
  10. int rec(int at, int where) {
  11.     if(at >= n) {
  12.         return 0;
  13.     }
  14.     if(dp[at][where] != -1) {
  15.         return dp[at][where];
  16.     }
  17.    
  18.     int res = 0;
  19.     if(where == 0 and at + 1 < n) {
  20.         if(A[at + 1] >= A[at]) {
  21.             res = max(res, rec(at + 1, where) + 1);
  22.         }
  23.         if(B[at + 1] >= A[at]) {
  24.             res = max(res, rec(at + 1, 1) + 1);
  25.         }
  26.     }
  27.     else if(where == 1 and at + 1 < n) {
  28.         if(A[at + 1] >= B[at]) {
  29.             res = max(res, rec(at + 1, 0) + 1);
  30.         }
  31.         if(B[at + 1] >= B[at]) {
  32.             res = max(res, rec(at + 1, where) + 1);
  33.         }
  34.     }
  35.    
  36.     dp[at][where] = res;
  37.     return res;
  38. }
  39. int main() {
  40.     ios_base::sync_with_stdio(false);
  41.     memset(dp, -1, sizeof dp);
  42.     cin >> n;
  43.    
  44.     for(int i = 0; i < n; i++) {
  45.         cin >> A[i];
  46.     }
  47.     for(int i = 0; i < n; i++) {
  48.         cin >> B[i];
  49.     }
  50.    
  51.     int res = 0;
  52.     for(int i = 0; i < n; i++) {
  53.         res = max(res, rec(i, 0) + 1);
  54.         res = max(res, rec(i, 1) + 1);
  55.     }
  56.     cout << res << endl;
  57.    
  58.     return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement