C/C++教程

NOIP 模拟 $36\; \rm Cicada 与排序$

本文主要是介绍NOIP 模拟 $36\; \rm Cicada 与排序$,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

设 \(rk_{i,j}\) 表示第 \(i\) 个数最后在相同的数里排第 \(j\) 位的概率。

转移时用一个 \(dp\),\(dp_{i,j,0/1}\) 表示归并排序时第一个数组弹了 \(i\) 个,第二个数组弹了 \(j\) 个,最后一个弹的是第一个数组的还是第二个的。

直接模拟归并排序,然后在过程中枚举值域即可。

Code
#include<bits/stdc++.h>
#define Re register
#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=0;x=0;register char ch=gc();
            while(!isdigit(ch)) f|=ch=='-',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 pb push_back
    #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;}
    typedef long long ll;
    static const int N=501,MOD=998244353,inv=499122177;
    std::vector<int> lT[N<<1],rT[N<<1];
    int a[N],lcnt[N<<1],rcnt[N<<1],mx,n;
    ll rk[N][N],tmp[N],dp[N][N][2];
    void mergesort(int l,int r) {
        if (l==r) return;
        int mid(l+r>>1);
        mergesort(l,mid);
        mergesort(mid+1,r);
        for (ri i(l);i<=mid;p(i)) ++lcnt[a[i]],lT[a[i]].pb(i);
        for (ri i(mid+1);i<=r;p(i)) ++rcnt[a[i]],rT[a[i]].pb(i);
        for (ri i(1);i<=mx;p(i)) {
            for (ri l(0);l<=lcnt[i];p(l)) 
                for (ri r(0);r<=rcnt[i];p(r)) dp[l][r][0]=dp[l][r][1]=0;
            dp[0][0][0]=1;
            for (ri l(0);l<=lcnt[i];p(l)) 
                for (ri r(0);r<=rcnt[i];p(r)) 
                    if (l!=lcnt[i]&&r!=rcnt[i]) 
                        dp[l+1][r][0]=(dp[l+1][r][0]+(dp[l][r][0]+dp[l][r][1])*inv)%MOD,
                        dp[l][r+1][1]=(dp[l][r+1][1]+(dp[l][r][0]+dp[l][r][1])*inv)%MOD;
                    else if (l!=lcnt[i]) dp[l+1][r][0]=(dp[l+1][r][0]+dp[l][r][0]+dp[l][r][1])%MOD;
                    else if (r!=rcnt[i]) dp[l][r+1][1]=(dp[l][r+1][1]+dp[l][r][0]+dp[l][r][1])%MOD;
            for (ri j(0);j<lcnt[i];p(j)) {
                for (ri k(1);k<=lcnt[i];p(k))
                    for (ri r(0);r<=rcnt[i];p(r)) tmp[k+r]=(tmp[k+r]+rk[lT[i][j]][k]*dp[k][r][0])%MOD;
                for (ri nrk(1);nrk<=lcnt[i]+rcnt[i];p(nrk)) rk[lT[i][j]][nrk]=tmp[nrk],tmp[nrk]=0;
            }
            for (ri j(0);j<rcnt[i];p(j)) {
                for (ri k(1);k<=rcnt[i];p(k))
                    for (ri l(0);l<=lcnt[i];p(l)) tmp[k+l]=(tmp[k+l]+rk[rT[i][j]][k]*dp[l][k][1])%MOD;
                for (ri nrk(1);nrk<=lcnt[i]+rcnt[i];p(nrk)) rk[rT[i][j]][nrk]=tmp[nrk],tmp[nrk]=0;
            }
            lT[i].clear(),rT[i].clear();
            lcnt[i]=rcnt[i]=0;
        }
    }
    inline int main() {
        //FI=freopen("nanfeng.in","r",stdin);
        //FO=freopen("nanfeng.out","w",stdout);
        cin >> n;
        for (ri i(1);i<=n;p(i)) cin >> a[i],mx=cmax(mx,a[i]);
        for (ri i(1);i<=n;p(i)) rk[i][1]=1;
        mergesort(1,n);
        for (ri i(1);i<=n;p(i)) ++lcnt[a[i]];
        for (ri i(1);i<=mx;p(i)) lcnt[i]+=lcnt[i-1];
        for (ri i(1);i<=n;p(i)) {
            Re ll ans(0);
            for (ri j(1);j<=lcnt[a[i]]-lcnt[a[i]-1];p(j)) ans=(ans+rk[i][j]*j)%MOD;
            printf("%lld ",ans+lcnt[a[i]-1]);
        }
        return 0;
    }
}
int main() {return nanfeng::main();}
这篇关于NOIP 模拟 $36\; \rm Cicada 与排序$的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!