コードフォース1367フライングソート

x、x + 1、x + 2、... x + kのようなサブシーケンスを見つけます。ここでは、すべての開始と終了を表示する必要はなく、すべてを中央に表示する必要が
あります。dpには3つのケースがあります

#include<bits/stdc++.h>
#define ls rt<<1
#define rs rt<<1|1
#define pb push_back
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef vector<int> VI;
const  ll inf  = 0x3f3f3f3f3f3f3f3f;
const int mod  = 1e9+7;
const int maxn = 1e6 + 4;
const int N    = 5e3 + 5;
const double eps = 1e-6;
const double pi = acos(-1.0);
ll qpow(ll x,ll y){
    
    ll ans=1;x%=mod;assert(y>=0);while(y){
    
     if(y&1) ans=ans*x%mod; x=x*x%mod; y>>=1;}return ans;}
//#define LOCAL
struct tupe{
    
    int x,y;};//tuple


int _,n,m,a[maxn],b[maxn],c[maxn],dp[maxn][3],l[maxn],r[maxn],lst[maxn];
int main() {
    
    
#ifdef LOCAL
    freopen("RACE input.in","r",stdin);
#endif
    for(scanf("%d",&_);_;_--) {
    
    
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i],l[i]=r[i]=c[i]=lst[i]=0;
        sort(b+1,b+1+n) ;m=unique(b+1,b+1+n)-b-1;
        for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+1+m,a[i])-b;
        for(int i=1;i<=n;i++) {
    
    
            r[a[i]]=i;  c[a[i]]++;
            if(!l[a[i]]) l[a[i]]=i;
            dp[i][0]=dp[i][1]=dp[i][2]=0;
        }
        for(int i=1;i<=n;i++) {
    
    
            dp[i][0]=dp[lst[a[i]]][0]+1;
            dp[i][1]=max(dp[lst[a[i]]][1],max(dp[lst[a[i]-1]][0],dp[lst[a[i]-1]][2]))+1;
            if(r[a[i]]==i) dp[i][2]=dp[l[a[i]]][1]+c[a[i]]-1;
            lst[a[i]]=i;
        }
        int mx=0;
        for(int i=1;i<=n;i++) mx=max(mx,max(max(dp[i][1],dp[i][2]),dp[i][0]));
        printf("%d\n",n-mx);
    }
}

おすすめ

転載: blog.csdn.net/qq_43914084/article/details/106909761