Codeforces 964G Almost Increasing Array 线段树 (看题解)

Almost Increasing Array

来讲讲自己想到的和没想到的。。

首先要把严格递增变成非严格递增每个元素只要减去所处位置的下标。

然后我们考虑枚举被删除的点, 被删点前面的每个元素减去 i , 被删的每个点后面的元素减去 (i - 1)

然后求LIS, 在所有的LIS里面的最大值, 这些就是最多的不用动的元素。 然后感觉枚举被删掉的点

感觉维护不了那么多信息, 感觉要枚举左边的每一个值为结尾取计算, 然后就GG了。。

感觉就一点没有想到, 就是我们枚举被删掉的点的时候, 假设被删掉的点的下标为 k , 那么我们只需要

考虑以k - 1 为结尾的前缀信息和后面的部分组成的LIS, 因为对于小于k - 1的下标 j , 我们在枚举 j + 1的时候计算就好了。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 5e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}

int n, a[N], preLen[N];
int hs[N], cnths;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1

struct segmentTree {
    int mx[N << 2];
    void init() {
        memset(mx, 0, sizeof(mx));
    }
    void update(int p, int val, int l, int r, int rt) {
        if(l == r) {
            chkmax(mx[rt], val);
            return;
        }
        int mid = l + r >> 1;
        if(p <= hs[mid]) update(p, val, lson);
        else update(p, val, rson);
        mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
    }
    int query(int L, int R, int l, int r, int rt) {
        if(R < hs[l] || hs[r] < L || R < L) return 0;
        if(L <= hs[l] && hs[r] <= R) return mx[rt];
        int mid = l + r >> 1;
        return max(query(L, R, lson), query(L, R, rson));
    }
} Tree;

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        hs[++cnths] = a[i] - i;
        hs[++cnths] = a[i] - i + 1;
    }
    sort(hs + 1, hs + 1 + cnths);
    cnths = unique(hs + 1, hs + 1 + cnths) - hs - 1;
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        preLen[i] = Tree.query(-inf, a[i] - i, 1, cnths, 1) + 1;
        Tree.update(a[i] - i, preLen[i], 1, cnths, 1);
    }
    ans = Tree.mx[1];
    Tree.init();
    for(int i = n - 1; i >= 2; i--) {
        int nexLen = Tree.query(a[i + 1] - i, inf, 1, cnths, 1) + 1;
        Tree.update(a[i + 1] - i, nexLen, 1, cnths, 1);
        chkmax(ans, preLen[i - 1] + Tree.query(a[i - 1] - i + 1, inf, 1, cnths, 1));
    }
    printf("%d
", max(0, n - ans - 1));
    return 0;
}

/*
*/
原文地址:https://www.cnblogs.com/CJLHY/p/10912248.html