CodeForces - 1437E Make It Increasing(LIS变形)


传送门:CodeForces - 1437E

思路分析

首先排除掉$b$数组的影响,答案显然是$n-LIS$,且这个$LIS$有限制,对于$j>i$,必须满足$a[j]>a[i]$并且$a[j]-a[i]>=j-i$
将两个式子转化一下一下,即每个数减去下标,此时只需要满足$a[i]<=a[j]$即可,如果加上$b$数组的影响,那么就看成每个区间内部求$LIS$

AC代码

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc() {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    template<class T> inline bool read(T &x) {
        bool sign=0;
        char ch=nc();
        x=0;
        for(; blank(ch); ch=nc());
        if(IOerror)return false;
        if(ch=='-')sign=1,ch=nc();
        for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if(sign)x=-x;
        return true;
    }
    template<class T,class... U>bool read(T& h,U&... t) {
        return read(h)&&read(t...);
    }
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;

const int INF = 0x3f3f3f3f;
const int N = 5e5+10;

int a[N],b[N],vis[N];
int n,m;

int work(int l,int r,int vl,int vr) {
    vector<int> f;
    for (int i = l; i <= r; ++i) {
        if (vl <= a[i] && a[i] <= vr) {
            auto it = std::upper_bound(f.begin(), f.end(), a[i]);
            if (it == f.end()) f.push_back(a[i]);
            else *it = a[i];
        }
    }
    return int(f.size());
}

int main() {

#ifdef xiaofan
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif


    cin>>n>>m;
    for(int i=1; i<=n; i++) cin>>a[i];
    for(int i=1; i<=m; i++) cin>>b[i];
    a[0] = -INF;
    a[n+1] = INF;
    b[0] = 0;
    b[m+1] = n+1;
    int ans = n-m;
    for(int i=1; i<=n; i++) a[i]-=i;
    for(int i=0; i<=m; i++) {
        if(a[b[i]]>a[b[i+1]]) {
            ans = -1;
            break;
        }
        ans -= work(b[i]+1,b[i+1]-1,a[b[i]],a[b[i+1]]);
    }
    cout<<ans<<endl;



    return 0;
}

&nbsp;

文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}