当前位置:网站首页>P4769 [NOI2018] Bubble Sort (Combinatorics)
P4769 [NOI2018] Bubble Sort (Combinatorics)
2022-07-29 18:33:00 【wind__whisper】
前言
Here is the linear approach.The nature of a few words in the solution to the problem was stunned for a whole morning.
too vegetable
解析
Consider what permutations are not legal.
A permutation if invalid,That is, during a certain exchange, the distance of one of the elements from the target does not decrease but increases,Then this number will be changed again in the future,That is, the number will jump repeatedly.
Consider how many numbers will hop over and over again,不难发现,Jumps repeatedly,It is equivalent to having an element on the left that is larger than itself,There are elements on the right that are smaller than itself,That is, there is a decreasing subsequence of length three.
Therefore, the necessary and sufficient conditions for legality can be abstracted:There are no decreasing subsequences of length three,也就等价于The permutation can be split into two increasing sequences.
这咋算啊?
hit the table,When it is found that there is no lexicographical order,The answer is the Cattelan number.
为什么呢?
Try to go up the um set.设 m x i = max j = 1 i a j mx_i=\max_{j=1}^ia_j mxi=maxj=1iaj,Then a permutation can be understood as all ( m x i , i ) (mx_i,i) (mxi,i) A path where the points are connected sequentially,It is not difficult to find that it is called the Cattelan number“ ( 0 , 0 ) − > ( n , n ) (0,0)->(n,n) (0,0)−>(n,n) and not more than the diagonal” The path is bijective.
Then this question is fine,Violently enumerates the first position larger than the given permutation,Then it must be updated at this time m x i mx_i mxi,设 f ( ( a , b ) − > ( c , d ) ) f((a,b)->(c,d)) f((a,b)−>(c,d)) 是从 ( a , b ) (a,b) (a,b) 走到 ( c , d ) (c,d) (c,d) and does not exceed the number of diagonal solutions(It can be tolerated and rejected by turning over O ( 1 ) O(1) O(1) 求解),So here's the number of options ∑ j = m x i + 1 n f ( ( j , i ) − > ( n , n ) ) = f ( ( m x i + 1 , i − 1 ) − > ( n , n ) ) \sum_{j=mx_i+1}^nf((j,i)->(n,n))=f((mx_i+1,i-1)->(n,n)) ∑j=mxi+1nf((j,i)−>(n,n))=f((mxi+1,i−1)−>(n,n)).Until the prefix of the given permutation must not be split into two increasing sequences is exit.
总复杂度 O ( n ) O(n) O(n).
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define ok debug("line: %d\n",__LINE__)
inline ll read(){
ll x(0),f(1);char c=getchar();
while(!isdigit(c)) {
if(c=='-')f=-1;c=getchar();}
while(isdigit(c)) {
x=(x<<1)+(x<<3)+c-'0';c=getchar();}
return x*f;
}
bool mem1;
const int N=2e6+100;
const int inf=1e9+100;
const int mod=998244353;
const bool Flag=0;
#define add(x,y) ((((x)+=(y))>=mod)&&((x)-=mod))
inline ll ksm(ll x,ll k){
ll res(1);
while(k){
if(k&1) res=res*x%mod;
x=x*x%mod;
k>>=1;
}
return res;
}
int n;
int a[N];
ll jc[N],ni[N];
void init(int n){
jc[0]=1;
for(int i=1;i<=n;i++) jc[i]=jc[i-1]*i%mod;
ni[n]=ksm(jc[n],mod-2);
for(int i=n-1;i>=0;i--) ni[i]=ni[i+1]*(i+1)%mod;
}
inline int C(int n,int m){
return n<m||m<0?0:jc[n]*ni[m]%mod*ni[n-m]%mod;
}
inline int walk(int i,int j){
return (C(n-i+n-j,n-i)+mod-C(n-1-i+n+1-j,n-1-i))%mod;
}
bool vis[N];
void work(){
n=read();
for(int i=1;i<=n;i++) a[i]=read(),vis[i]=0;
int mx(0),sec(1);
int ans(0);
for(int i=1;i<=n;i++){
vis[a[i]]=1;
if(a[i]>mx) mx=a[i];
else if(a[i]!=sec){
add(ans,walk(mx+1,i-1));
break;
}
while(vis[sec]) ++sec;
add(ans,walk(mx+1,i-1));
}
printf("%lld\n",ans);
}
bool mem2;
signed main(){
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
#endif
init(2e6);
int T=read();
while(T--) work();
return 0;
}
边栏推荐
- Internet Explorer 结束了它 26 年作为顶级浏览器的历史角色
- [Network] LAN technology MSTP
- Xatlas source code parsing (7)
- [High Concurrency] I used multithreading to optimize the massive data proofreading system under the billion-level traffic e-commerce business, and the performance was directly improved by 200%!!(The w
- go的堆内存结构分析
- Batch_Normalization 、Layer_Normalization 、Group_Normalization你分的清楚吗
- Babbitt | Metaverse Daily Must Read: Seven consecutive quarters of losses, Meta Metaverse division Q2 loss of $ 2.8 billion, Zuckerberg said this situation may continue for years ...
- 生物JC TRIM37防止凝集物组织的异位纺锤体极点的形成,以确保有丝分裂的保真度
- Dialogue with Academician Yu Fei of the Canadian Academy of Engineering: Looking for "Shannon's Theorem" in the field of AI
- 利用 JMeter 压测上传和下载接口实战
猜你喜欢
随机推荐
NFTScan and PANews jointly release multi-chain NFT data analysis report
【码蹄集新手村600题】pow()函数详解
Fast Reed-Solomon Interactive Oracle Proofs of Proximity学习笔记
如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然
不同的 DAO 对世界带来的改变
泰山OFFICE技术讲座:影响文字效果的因素,由四大升级为五大
Lanzhou Mencius Lightweight Pre-training Model Technical Practice
【南瓜书ML】(task5)支持向量机的数学推导(更新ing)
InstallAnywhere 2022
[High Concurrency] I used multithreading to further optimize the massive data proofreading system under the billion-level traffic e-commerce business, and the performance has been improved by 200% aga
Flink on yarn双流join问题分析+性能调优思路
多智能体协同控制研究中光学动作捕捉与UWB定位技术比较
unbuntu18.04-----bilibili网页端无法播放视频
[网络]IPv6基础IPv6概述
MySQL数据库基础
抗HER2/neu受体拟肽修饰的紫杉醇自蛋白纳米粒/环境敏感型多糖纳米粒的制备,
详解析构函数、拷贝构造函数
hihoCoder#: 博弈游戏·Nim游戏
硬核!世界顶级级架构师编写2580页DDD领域驱动设计笔记,也太强了!
Pagination with LIMIT









