题解:AT_abc478_f [ABC478F] Min-First Search
考虑对每个点求合法父亲的数量。
一个显然的条件是
时间复杂度
#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;
}/*
*/
:::