跳到正文

题解

题解:AT_abc478_f [ABC478F] Min-First Search

hiebb约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年10月4日
收录
2026年10月4日
更新
2026年11月13日
篇幅
约 2 分钟
获赞
1

版本

  1. v2当前
  2. v1

考虑对每个点求合法父亲的数量。

一个显然的条件是 fi<if_i<i,但这样不一定合法。考虑一个 j<i,Qj>Qij<i,Q_j>Q_i,则 Qi,QjQ_i,Q_j 一定不能在一同在 SS 中出现,否则会先取出 QiQ_i 而非 QjQ_j。因此,还需要满足 fi≥lif_i\ge l_i,其中 lil_i 为上述最大的 jj,不存在则为 11。最后的答案就是 i−lii-l_i 的积。lil_i 用单调栈维护即可。

时间复杂度 O(n)O(n)。

code
C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+5,mod=998244353;
int n,q[N],ans=1;
stack<int>st;
signed main(){
    // freopen(".in","r",stdin);
    // freopen(".out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>q[i];
    for(int i=1;i<=n;i++){
        while(!st.empty()&&q[st.top()]<q[i]) st.pop();
        if(i>1) ans=1ll*ans*(i-(st.empty()?1:st.top()))%mod;
        st.push(i);
    }
    cout<<ans<<'\n';
    return 0;
}/*
*/
原文
存档评论1 条抓取于