当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Which is better, traditional render farm or cloud render farm?
如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然
4G无线模块 电力通信模块
unity-shader-游戏渲染效果逆向分析
go的堆内存结构分析
浅析无人机发展趋势以及如何实现EasyDSS+无人机视频推流?
清道夫受体-A靶向脂肪酸修饰白蛋白纳米粒/银耳多糖修饰白蛋白微球的制备
canvas随机生成树木js特效
Batch_Normalization 、Layer_Normalization 、Group_Normalization你分的清楚吗
Live '| 37 mobile game analysis of how to implement user use StarRocks portrait
随机推荐
利用 JMeter 压测上传和下载接口实战
在中国 ToB 市场,选一个对的供应商太难了
【南瓜书ML】(task5)支持向量机的数学推导(更新ing)
提高编程效力的5大VS Code Extensions
休息天的早晨感觉不到在休息
kubernetes之资源限制及QOS服务质量
crontab执行定时任务报错的问题
解决报错Unsupported field: HourOfDay
Interviewer: How does MySQL tune SQL statements based on execution plans?
[Network knowledge] Routing OSPF
redis cluster 集群,终极方案?
P4769 [NOI2018] 冒泡排序(组合数学)
[网络]WAN技术组播
【码蹄集新手村600题】求空间直角坐标系中俩点之间的距离
详解析构函数、拷贝构造函数
unity-shader-游戏渲染效果逆向分析
Domino服务器SSL证书安装指南
JupyterNotebook安装插件管理包过程中报错( AttributeError module ‘tornado.web‘ has no attribute ‘asynchronous‘ )
js选择多张图片对比功能插件
【回忆】奶奶的歌谣