题目链接: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;
}