题解:AT_abc439_f [ABC439F] Beautiful Kadomatsu

· · 题解

题意简述

给定一个长度为 N 的排列 P,统计 P 的所有子序列中满足门松条件的个数,并对 998244353 取模输出。

门松序列定义: 设序列 a = (a_1, a_2, \ldots, a_k),定义:

当且仅当 x > y 时,称 a 是门松的。

输出满足条件的子序列个数对 998244353 取模的结果。

思路

可以观察到对于任意长度 k \ge 3 的序列 a = (a_1, a_2, \ldots, a_k),其峰谷数量差 x - y 完全由序列的首尾趋势决定:

  1. "上升" 开始,"上升" 结束(模式:\nearrow \cdots \nearrow)

    • 特征:a_1 < a_2 且 a_{k-1} < a_k。
    • 结果:x = y。
  2. "下降" 开始,"下降" 结束(模式:\searrow \cdots \searrow)

    • 特征:a_1 > a_2 且 a_{k-1} > a_k。
    • 结果:x = y。
  3. "下降" 开始,"上升" 结束(模式:\searrow \cdots \nearrow)

    • 特征:a_1 > a_2 且 a_{k-1} < a_k。
    • 结果:x = y - 1(即 y > x)。
  4. "上升" 开始,"下降" 结束(模式:\nearrow \cdots \searrow)

    • 特征:a_1 < a_2 且 a_{k-1} > a_k。
    • 结果:x = y + 1(即 x > y)。

所以我们可得出序列 a 是门松序列(x > y)的充要条件:

\begin{cases} a_1 < a_2 \\ a_{k-1} > a_k \end{cases}

这就可以将复杂的峰谷计数问题转化为简单的首尾条件判断:

做法

设子序列在原排列中的位置为 i_1, i_2, \ldots, i_k,关注两个关键位置:

情况 A:l = r(长度 = 3)

此时子序列为 (a_1, a_2, a_3),要求 a_1 < a_2 > a_3。

令:

贡献:\text{ans}_A = \sum_{l=1}^n pre[l] \times suf[l]。

情况 B:l < r(长度 ≥ 4)

贡献:\text{ans}_B = \sum_{l=1}^{n-1} \sum_{r=l+1}^n pre[l] \times suf[r] \times 2^{r-l-1}。

公式变形:

\begin{aligned} \text{ans}_B &= \sum_{l=1}^{n-1} pre[l] \times 2^{-(l+1)} \times \sum_{r=l+1}^n (suf[r] \times 2^r) \\ &= \sum_{l=1}^{n-1} pre[l] \times inv[l+1] \times sum[l+1] \end{aligned}

其中:

时间复杂度:O(N \log N)。

Code:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e5+5;
const int M=998244353;
int n,p[N],pre[N],suf[N],sum[N],pow2[N],inv[N];
struct BIT{
    int c[N],n;
    inline void init(int k){
        n=k;
        memset(c,0,sizeof c);
    }
    inline int lowbit(int x){
        return x&(-x);
    }
    inline void update(int x,int d){
        for(;x<=n;x+=lowbit(x)){
            c[x]+=d;
        }
    }
    inline int ask(int x){
        int res=0;
        for(;x;x-=lowbit(x)){
            res+=c[x];
        }
        return res;
    }
}bit;
int qpow(int a,int b){
    int res=1;
    while(b){
        if(b&1)res=res*a%M;
        a=a*a%M;
        b>>=1;
    }
    return res;
}
signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>p[i];
    }
    //预处理幂和逆元   
    pow2[0]=inv[0]=1;
    int inv2=qpow(2,M-2);
    for(int i=1;i<=n;i++){
        pow2[i]=pow2[i-1]*2%M;
        inv[i]=inv[i-1]*inv2%M;
    }
    //计算pre数组:每个位置左边小于它的数的个数 
    bit.init(n);
    for(int i=1;i<=n;i++){
        pre[i]=bit.ask(p[i]-1);
        bit.update(p[i],1);
    }
    //计算suf数组:每个位置右边小于它的数的个数 
    bit.init(n);
    for(int i=n;i>=1;i--){
        suf[i]=bit.ask(p[i]-1);
        bit.update(p[i],1);
    }
    //计算后缀和:sum[i] = Σ_{r=i}^n (suf[r] * 2^r)
    for(int i=n;i>=1;i--){
        sum[i]=(sum[i+1]+suf[i]*pow2[i]%M)%M;
    }
    int ans=0;
    for(int l=1;l<=n;l++){
        //长度为3的情况 (l == r)
        ans=(ans+pre[l]*suf[l]%M)%M;
        //长度大于3的情况 (l < r),计算 pre[l] * Σ_{r=l+1}^n (suf[r] * 2^(r-l-1))
        ans=(ans+pre[l]*sum[l+1]%M*inv[l+1]%M)%M;
    }
    cout<<ans;
}