Loading

NOIP 模拟 $30\; \rm 毛二琛$

题解 \(by\;zj\varphi\)

原题问的就是对于一个序列,其中有的数之间有大小关系限制,问有多少种方案。

\(dp_{i,j}\) 表示在前 \(i\) 个数中,第 \(i\) 个的排名为 \(j\)的方案数

方程:

\[f_{i,j}=\begin{cases} \sum\limits_{k=j}^{i-1} f_{i-1,k},(p_{i-1}<p_i)\\ \sum\limits_{k=1}^{j-1} f_{i-1,k},(p_{i-1}>p_i)\\ \end{cases} \]

直接前缀和优化即可 \(\mathcal O\rm(n^2)\)

Code
#include<bits/stdc++.h>
#define ri register signed
#define p(i) ++i
namespace IO{
    char buf[1<<21],*p1=buf,*p2=buf;
    #define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
    struct nanfeng_stream{
        template<typename T>inline nanfeng_stream &operator>>(T &x) {
            ri f=1;x=0;register char ch=gc();
            while(!isdigit(ch)) {if (ch=='-') f=0;ch=gc();}
            while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
            return x=f?x:-x,*this;
        }
    }cin;
}
using IO::cin;
namespace nanfeng{
    #define FI FILE *IN
    #define FO FILE *OUT
    template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
    template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
    static const int N=5e3+7,MOD=1e9+7;
    int dp[N][N],g[N][N],a[N],n,ans;
    bool mv[N];
    inline int main() {
        //FI=freopen("nanfeng.in","r",stdin);
        //FO=freopen("nanfeng.out","w",stdout);
        cin >> n;
        for (ri i(0);i<n;p(i)) cin >> a[i];
        for (ri i(0);i<n;p(i)) 
            if (i<a[i]) {
                if (i) mv[i-1]=1;
                mv[a[i]-1]=1;
            } else for (ri j(a[i]);j<i-1;p(j)) mv[j]=1;
        dp[0][1]=g[0][1]=1;
        for (ri i(1);i<n-1;p(i)) {
            for (ri j(1);j<=i+1;p(j)) {
                if (mv[i-1]) dp[i][j]=(dp[i][j]+g[i-1][i]-g[i-1][j-1]+MOD)%MOD;       
                else dp[i][j]=(dp[i][j]+g[i-1][j-1])%MOD;
                g[i][j]=(g[i][j-1]+dp[i][j])%MOD;
            }
        }
        for (ri i(1);i<n;p(i)) ans=(ans+dp[n-2][i])%MOD;
        printf("%d\n",ans);
        return 0;
    }
}
int main() {return nanfeng::main();}
posted @ 2021-08-06 21:41  ナンカエデ  阅读(27)  评论(0编辑  收藏  举报