[NOI2018] Bubble Sort

[NOI2018] Bubble Sort


God questions orz

This limitation in the subject does not exist actually decrease the length of the sequence 3

Look no lexicographical requirements how to do it.

First \ (O (n ^ 2) \) recursive formula (I was too weak it will not ...)

Absence of sequences of length 3, then the drop sequence can be divided into two rising sequences.

\ (f [i] [j ] \) represents \ (I \) number has (J \) \ maxima is greater than ever filled.

Since the rise can be divided into two sequences, less than the maximum number of these filled through the fill will be small to large (rounded into a rising sequence) can easily fill larger than

Then transfer equation is \ (f [i] [j ] = \ sum_ {k \ leq j} f [i-1] [k] \)

\ (f [i-1] [j] \) transferred from a filling is less than the maximum filled out, otherwise fill \ (1 \) one greater than the current maximum, satisfying all greater than the current maximum number of in (small to large) ranked \ (jk \)

Another condition \ (I \ Leq J \) , where the boundary \ (F [0] [0] =. 1 \) .

And a prefix transfer equation can be rewritten as \ (F [I] [J] = F [. 1-I] [J] + F [I] [J-. 1] \) .

So that is a combination of mathematical set theory, from \ ((0,0) \) took only right went \ ((i, j) \ ) does not touch the line \ (y = x + 1 \ ) of program number, apparently \ (F [I] [J] = I + J-C_ {}. 1. 1-I ^ {} {-C_-I + J + I. 1. 1} ^ {} \) , the answer is \ (f [n] [n] \)

Then there lexicographical requirements how to do, p represents the lexicographical lower bound, q represents the determined order. Enumeration \ (I \) , lexicographic required to satisfy \ (\ FORALL. 1 \ Leq J <I, p_j = Q_j; P_i <Q_I \) . Set \ (maxp = \ J = max_. 1} ^ {ip_i \) , the front and has checked \ (i-1 \) a \ (P \) reasonable. Category talk:

  1. \ (Q_I <maxp \) , can be found not work, because the first \ (i-1 \) a \ (p \) should be reasonable, less than the current maximum number of fill from just over dp know will fill Motian get the minimum number. \ (p_i \) apparently not filled out this Motian flies decimal upper bound is \ (p_i \) .
  2. \ (Q_I> maxp \) , then the number of the program after to statistics, can be found in \ (f [Ni] [the n--Q_I] \) . And the sum or prefix, \ (F [n--I +. 1] [n---maxp. 1] \) .

Then you want to check the newly added \ (p \) is reasonable, if the new \ (p \) is not the maximum checks \ (p \) minimum is whether Motian off of numbers. Fucks on the line.

#include<bits/stdc++.h>
#define il inline
#define vd void
#define mod 998244353
typedef long long ll;
il ll gi(){
    ll x=0,f=1;
    char ch=getchar();
    while(!isdigit(ch))f^=ch=='-',ch=getchar();
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f?x:-x;
}
il int pow(int x,int y){
    int ret=1;
    while(y){
        if(y&1)ret=1ll*ret*x%mod;
        x=1ll*x*x%mod;y>>=1;
    }
    return ret;
}
int n,p[600010],t[600010];
int fact[1200010],ifact[1200010];
il int C(int n,int m){return 1ll*fact[n]*ifact[m]%mod*ifact[n-m]%mod;}
il int F(int i,int j){return (C(i+j-1,i-1)-C(i+j-1,i+1)+mod)%mod;}
int main(){
    int T=gi(),nn=1200000;
    fact[0]=1;for(int i=1;i<=nn;++i)fact[i]=1ll*fact[i-1]*i%mod;
    ifact[nn]=pow(fact[nn],mod-2);for(int i=nn-1;~i;--i)ifact[i]=1ll*ifact[i+1]*(i+1)%mod;
    while(T--){
        n=gi();
        for(int i=1;i<=n;++i)p[i]=gi();
        int maxp=0,ans=0;
        for(int i=1,s=0;i<=n;++i){
            maxp=std::max(p[i],maxp);
            if(maxp==n)break;
            ans=(ans+F(n-i+1,n-maxp-1))%mod;
            if(p[i]!=maxp&&s+1!=p[i])break;
            t[p[i]]=1;while(t[s+1])++s;
        }
        printf("%d\n",ans);
        for(int i=1;i<=n;++i)t[i]=0;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/xzz_233/p/10993623.html