Beingamanforever

DZY Loves Sequences

Dec 8th, 2024 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. /**
  2.  *    author:  compounding
  3.  *    created: 2024-12-08 14:46:24
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define yes cout << "YES" << endl
  21. #define no cout << "NO" << endl
  22. #define endl "\n"
  23. const int mod = 1000000007;
  24. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  25. void solve()
  26. {
  27.     // TODO: LIS, with atmost 1 change to make it strictly increasing
  28.     int n;
  29.     cin >> n;
  30.     vi a(n);
  31.     for (int i = 0; i < n; i++)
  32.     {
  33.         cin >> a[i];
  34.     }
  35.     vi lis_ending_at_i(n, 1);
  36.     vi lis_starting_from_i(n, 1);
  37.     vi L; // seq array
  38.     L.push_back(a[0]);
  39.     for (int i = 0; i < n; i++)
  40.     {
  41.         auto it = lower_bound(all(L), a[i]);
  42.         int length = it - L.begin();
  43.         lis_ending_at_i[i] = length + 1;
  44.         if (it == L.end())
  45.         {
  46.             L.push_back(a[i]);
  47.         }
  48.         else
  49.         {
  50.             *it = a[i];
  51.         }
  52.     }
  53.     vi R;
  54.     R.push_back(a[n - 1]);
  55.     for (int i = n - 1; i >= 0; i--)
  56.     {
  57.         auto it = lower_bound(all(R), a[i]);
  58.         int length = it - R.begin();
  59.         lis_starting_from_i[i] = length + 1;
  60.         if (it == R.end())
  61.         {
  62.             R.push_back(a[i]);
  63.         }
  64.         else
  65.         {
  66.             *it = a[i];
  67.         }
  68.     }
  69.     int ans = 1;
  70.     for (int i = 0; i < n; i++)
  71.     {
  72.         if (i && i < n - 1)
  73.         {
  74.             if (a[i - 1] + 1 < a[i + 1])
  75.             {
  76.                 // we change the element at i, to (a[i-1]+1)
  77.                 ans = max({ans, lis_ending_at_i[i - 1] + lis_starting_from_i[i + 1] + 1});
  78.             }
  79.         }
  80.         if (i)
  81.         {
  82.             ans = max(ans, lis_ending_at_i[i - 1] + 1);
  83.         }
  84.         if (i < n - 1)
  85.         {
  86.             ans = max(ans, lis_starting_from_i[i - 1] + 1);
  87.         }
  88.     }
  89.     cout << ans + 1 << endl;
  90.     return;
  91. }
  92.  
  93. signed main()
  94. {
  95.     NeedForSpeed;
  96.     int t = 1;
  97.     // cin >> t;
  98.     while (t--)
  99.     {
  100.         solve();
  101.     }
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment