CEOI2018 Global warming 题解
P5978 [CEOI 2018] Global warming 题解
题目传送门
考虑对于一个区间加上或减去一个正整数会对整个序列的 LIS 产生怎样的影响。
假设我们对于区间
因此如果想要对一个区间减少一个正整数值
我们再来考虑对区间进行加法,使用同样的办法分析,不难发现,对于一个区间
还有一个需要分析的点是,减去的正整数值
我们考虑如何去做这件事情,暴力修改并求 LIS 的时间复杂度是
发现对区间
贴代码。
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int maxn = 2e5 + 10;
int a[maxn];
int f[maxn],l[maxn];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int ans = 0;
memset(f,0x3f,sizeof(f));
int n,x;
cin >> n >> x;
for(int i = 1;i <= n;i++) cin >> a[i];
for(int i = 1;i <= n;i++)
{
int nowx = lower_bound(f,f + n,a[i]) - f;
f[nowx] = a[i];
l[i] = nowx + 1;
ans = max(ans,l[i]);
}
memset(f,0x3f,sizeof(f));
for(int i = n;i >= 1;i--)
{
int nowx = lower_bound(f,f + n,-a[i] + x) - f;
int nowl = lower_bound(f,f + n,-a[i]) - f;
f[nowl] = -a[i];
ans = max(ans,l[i] + nowx);
}
cout << ans << endl;
return 0;
}