題目連結:Atcoder 或者 洛谷
來講講純降智做法,不需要任何智商的做法,順帶整活:
對於一個 \(LIS\) 可以拆成 \(preLIS+sufLIS\),而我們現在至多可以修改一個點,那麼如果 \(preLIS\) 的末尾元素為 \(x\),\(sufLIS\) 的末尾元素為 \(y\),那麼如果有 \(y-x\ge 2\),那麼我們可以至少找到一個元素 \(v\) 使得 \(x<v<y\),那麼如果 \(preLIS\) 與 \(sufLIS\) 中有空位,我們就可以任取一個一個元素變為 \(v\) 從而得到更長的 \(LIS\),在原來的基礎上 \(+1\)。
這啟發我們預處理出 \(l[i]\) 以 \(i\) 結尾的 \(LIS\)。\(r[i]\) 以 \(i\) 開始的 \(LIS\)。
那麼我們隨便列舉一個,比如列舉 \(r[i]\),然後 \(1 \sim i-2\) 的 \(l[i]\) 都是可選的,因為我們至少需要保留一個空位 \(i-1\) 使得至少有一個運算元可以用來修改為 \(v\),而顯然的是,我們只需要找到 \(\le a[i]-2\) 的最大 \(l[i]\) 就可以和 \(r[i]\) 以及中間這個數改為 \(v\) 拼成 \(l_{max}+r[i]+1\) 了。
現在考慮預處理,顯然關於值域的樹狀陣列就行了,不想離散化?那就整活用雜湊表維護軸,這樣一來就可以實現動態開點樹狀陣列了。
考慮查詢時,每次都只需要 \(1 \sim i-2\) 處的 \(l[i]\) 進入樹狀陣列,而 \(i\) 是從大到小列舉就行了。那麼我們可以使用可撤銷代替刪除,因為我們的增加為:
加入 \((a[i],l[i])\) 維護關於 \(a[i]\) 的值域最大值,最大值顯然是無法被刪除的,但可以用撤銷代替。
所以我們將 \(1 \sim n\) 的資訊依次加入到樹狀陣列當中,並且用一個棧維護修改的資訊,每次刪除就可以用撤銷代替了。
參照程式碼
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用於Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c) if (b & 1) (ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-') sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char)) return;
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow) return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3()
{
one = tow = three = 0;
}
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y) x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y) x = y;
}
struct Hash
{
static uint64_t splitmix64(uint64_t x)
{
x += 0x9e3779b97f4a7c15;
x = (x ^ x >> 30) * 0xbf58476d1ce4e5b9;
x = (x ^ x >> 27) * 0x94d049bb133111eb;
return x ^ x >> 31;
}
static size_t get(const uint64_t x)
{
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
template <typename T>
size_t operator()(T x) const
{
return get(std::hash<T>()(x));
}
template <typename F, typename S>
size_t operator()(pair<F, S> p) const
{
return get(std::hash<F>()(p.first)) ^ std::hash<S>()(p.second);
}
};
constexpr int N = 2e5 + 10;
constexpr int MX = INT_MAX;
int l[N], r[N];
int n, a[N];
int ans;
gp_hash_table<int, int, Hash> bit;
stack<pii> back;
int cnt[N];
inline void add(ll x, const int v, const bool isBack = false)
{
while (x <= MX)
{
if (isBack) back.emplace(x, bit[x]);
uMax(bit[x], v), x += lowBit(x);
}
}
inline int query(int x)
{
int ans = 0;
while (x)
{
if (bit.find(x) != bit.end()) uMax(ans, bit[x]);
x -= lowBit(x);
}
return ans;
}
inline void solve()
{
cin >> n;
forn(i, 1, n) cin >> a[i];
forn(i, 1, n)
{
const int mx = query(a[i] - 1);
l[i] = mx + 1;
add(a[i], l[i]);
}
bit.clear();
forv(i, n, 1)
{
const int mx = query(MX - a[i] - 1);
r[i] = mx + 1;
add(MX - a[i], r[i]);
}
forn(i, 1, n)
{
cnt[i] = back.size();
add(a[i], l[i], true);
}
forv(i, n, 1)
{
uMax(ans, l[i] + (i + 1 <= n));
uMax(ans, r[i] + (i - 1 >= 1));
if (i > 2)
{
while (back.size() > cnt[i - 1])
{
const auto [x,val] = back.top();
bit[x] = val;
back.pop();
}
const int mx = query(a[i] - 2);
uMax(ans, mx + r[i] + 1);
}
}
cout << ans;
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test) solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}