Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * author: compounding
- * created: 2024-12-08 14:46:24
- **/
- #include <bits/stdc++.h>
- using namespace std;
- mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
- #define NeedForSpeed \
- ios_base::sync_with_stdio(false); \
- cin.tie(NULL); \
- cout.tie(NULL);
- #define int long long
- #define all(x) (x).begin(), (x).end()
- typedef vector<int> vi;
- typedef vector<bool> vb;
- typedef vector<vi> vvi;
- typedef vector<pair<int, int>> vpi;
- #define f first
- #define s second
- #define yes cout << "YES" << endl
- #define no cout << "NO" << endl
- #define endl "\n"
- const int mod = 1000000007;
- int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
- void solve()
- {
- // TODO: LIS, with atmost 1 change to make it strictly increasing
- int n;
- cin >> n;
- vi a(n);
- for (int i = 0; i < n; i++)
- {
- cin >> a[i];
- }
- vi lis_ending_at_i(n, 1);
- vi lis_starting_from_i(n, 1);
- vi L; // seq array
- L.push_back(a[0]);
- for (int i = 0; i < n; i++)
- {
- auto it = lower_bound(all(L), a[i]);
- int length = it - L.begin();
- lis_ending_at_i[i] = length + 1;
- if (it == L.end())
- {
- L.push_back(a[i]);
- }
- else
- {
- *it = a[i];
- }
- }
- vi R;
- R.push_back(a[n - 1]);
- for (int i = n - 1; i >= 0; i--)
- {
- auto it = lower_bound(all(R), a[i]);
- int length = it - R.begin();
- lis_starting_from_i[i] = length + 1;
- if (it == R.end())
- {
- R.push_back(a[i]);
- }
- else
- {
- *it = a[i];
- }
- }
- int ans = 1;
- for (int i = 0; i < n; i++)
- {
- if (i && i < n - 1)
- {
- if (a[i - 1] + 1 < a[i + 1])
- {
- // we change the element at i, to (a[i-1]+1)
- ans = max({ans, lis_ending_at_i[i - 1] + lis_starting_from_i[i + 1] + 1});
- }
- }
- if (i)
- {
- ans = max(ans, lis_ending_at_i[i - 1] + 1);
- }
- if (i < n - 1)
- {
- ans = max(ans, lis_starting_from_i[i - 1] + 1);
- }
- }
- cout << ans + 1 << endl;
- return;
- }
- signed main()
- {
- NeedForSpeed;
- int t = 1;
- // cin >> t;
- while (t--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment