题意:
给你一个数组,问最多改变多少个数字,可以使他变成“几乎上升”,“几乎上升”的意思是最多删掉一个数后数组严格上升。
题解:
如果没有删除,
假如不是严格上升,我们可以用n - LIS(LIS的意思是最长不下降子序列)。
是严格上升,我们先把a[i] - i,然后再n - LIS。
现在的问题在于删除一个数后怎么办呢?
本来求LIS是用dp记录长度为i的结尾的最小值,
现在加一个记录删除一个数以后长度为i的最小值就行了。
转移看代码。
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <bitset>
#include <map>
#include <vector>
#include <stack>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#ifdef LOCAL
#define debug(x) cout<<#x<<" = "<<(x)<<endl;
#else
#define debug(x) 1;
#endif
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define lowbit(x) x&-x
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, int> pii;
const ll MOD = 1e9 + 7;
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 2e5 + 5;
int a[MAXN];
int d[2][MAXN];
int main() {
#ifdef LOCAL
freopen ("input.txt", "r", stdin);
#endif
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
a[i] -= i;
}
memset(d, 0x3f, sizeof(d));
int pre = 0;
for (int i = 1; i <= n; i++) {
int pos1 = upper_bound(d[0] + 1, d[0] + 1 + i, a[i]) - d[0];
int pos2 = upper_bound(d[1] + 1, d[1] + 1 + i, a[i] + 1) - d[1];
d[1][pre] = min(d[0][pre], d[1][pre]);
d[1][pos2] = a[i] + 1;
d[0][pre = pos1] = a[i];
}
int pos1 = lower_bound(d[0] + 1, d[0] + 1 + n, INF) - d[0] - 1;
int pos2 = lower_bound(d[1] + 1, d[1] + 1 + n, INF) - d[1] - 1;
printf("%d\n", max(0, min(n - pos1, n - 1 - pos2)));
return 0;
}