当前位置:网站首页>P4769-[noi2018] bubble sort [combinatorics, tree array]
P4769-[noi2018] bubble sort [combinatorics, tree array]
2022-06-29 08:39:00 【QuantAsk】
On the subject
Topic link :https://www.luogu.com.cn/problem/P4769
The main idea of the topic
There is a bubble sort algorithm
Input : A length of n Permutation p[1...n]
Output :p The result after sorting .
for i = 1 to n do
for j = 1 to n - 1 do
if(p[j] > p[j + 1])
In exchange for p[j] And p[j + 1] Value
Then give an arrangement a a a, Find the order greater than in all dictionaries a a a Permutation p p p The number of bubble sort exchanges in is exactly ∑ i = 1 n ∣ i − p i ∣ \sum_{i=1}^n|i-p_i| ∑i=1n∣i−pi∣ Number of permutations of .
1 ≤ n ≤ 6 × 1 0 5 , ∑ n ≤ 2 × 1 0 6 1\leq n\leq 6\times 10^5,\sum n\leq 2\times 10^6 1≤n≤6×105,∑n≤2×106
Their thinking
It is found that the legal arrangement condition is that the longest descending subsequence does not exceed 2 2 2.
Then let's not consider what the lexicographic order constraint does , We set up f i , 1 / 2 f_{i,1/2} fi,1/2 Indicates that the current descending subsequence length is 1 / 2 1/2 1/2 The biggest one at the end of the middle .
that f i , 1 f_{i,1} fi,1 It is the largest number in the world , Then if we fill in the numbers from front to back , So if ≤ f i , 2 \leq f_{i,2} ≤fi,2 Is there anything in the number of , It's definitely not legal , therefore f i , 2 f_{i,2} fi,2 It must be smaller than all the figures that have not been filled in at present , There is no need to consider .
set up g i , j g_{i,j} gi,j It means that there are still i i i The number is not filled in , among f i , 1 f_{i,1} fi,1 Greater than j j j Number , So there are g i , j g_{i,j} gi,j Can be transferred to g i − 1 , j − 1 g_{i-1,j-1} gi−1,j−1( Fill in the bottom ) and g i − 1 , k ( k ≥ j ) g_{i-1,k}(k\geq j) gi−1,k(k≥j)( Fill in j j j above ).
We consider quickly finding each g g g, The reverse is g i , j g_{i,j} gi,j Transferred to the g i + 1 , j + 1 g_{i+1,j+1} gi+1,j+1 and g i + 1 , j g_{i+1,j} gi+1,j.
We maintain a h i , j = g i , i − j h_{i,j}=g_{i,i-j} hi,j=gi,i−j, So every shift is h i , j h_{i,j} hi,j Transferred to the h i , k ( j ≤ k ≤ i ) h_{i,k}(j\leq k\leq i) hi,k(j≤k≤i)
This transfer is very much like the Cartland number , You can go down or right each time , But not beyond the diagonal .
So move to position ( n , m ) ( m ≤ n ) (n,m)(m\leq n) (n,m)(m≤n) The number of schemes is ( n + m m ) − ( n + m m − 1 ) \binom{n+m}{m}-\binom{n+m}{m-1} (mn+m)−(m−1n+m).
Then it enumerates the first position beyond the dictionary order , So the previous scheme is fixed , The remaining number can be calculated from the tree array , Then use the combination number to find the answer .
Time complexity : O ( n log n ) O(n\log n) O(nlogn)
code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define lowbit(x) (x&-x)
using namespace std;
const ll N=6e5*2,P=998244353;
ll T,n,t[N],a[N],fac[N],inv[N],ans;
ll C(ll n,ll m)
{
if(m<0)return 0;return fac[n]*inv[m]%P*inv[n-m]%P;}
void Change(ll x,ll val){
while(x<=n){
t[x]+=val;
x+=lowbit(x);
}
return;
}
ll Ask(ll x){
ll ans=0;
while(x){
ans+=t[x];
x-=lowbit(x);
}
return ans;
}
ll F(ll n,ll m){
m=n-1-m;if(!m)return 0;m--;
return (C(n+m,m)-C(n+m,m-1)+P)%P;
}
signed main()
{
// freopen("inverse3.in","r",stdin);
fac[0]=inv[0]=inv[1]=1;
for(ll i=2;i<N;i++)inv[i]=P-inv[P%i]*(P/i)%P;
for(ll i=1;i<N;i++)fac[i]=fac[i-1]*i%P,inv[i]=inv[i-1]*inv[i]%P;
scanf("%lld",&T);
while(T--){
scanf("%lld",&n);ans=0;
for(ll i=1;i<=n;i++)
scanf("%lld",&a[i]),Change(a[i],1);
for(ll i=1,mx=0;i<=n;i++){
Change(a[i],-1);mx=max(mx,a[i]);
(ans+=F(n-i+1,Ask(mx)))%=P;
if(a[i]<mx&&Ask(a[i]))
break;
}
for(int i=1;i<=n;i++)t[i]=0;
printf("%lld\n",ans);
}
return 0;
}
边栏推荐
- 开发小技巧-图片资源管理
- A review of visual SLAM methods for autonomous driving vehicles
- mysql 主键约束删除问题
- 802.11--802.11n协议 PHY
- Differences between x86 and x64
- 目标跟踪【单目标跟踪(VOT/SOT)、目标检测(detection)、行人重识别(Re-ID)】
- 搭建开源物联网平台教程
- 对比HomeKit、米家,智汀家庭云版有哪些场景化的体验
- Déclaration de la variable Typescript - - assertion de type
- Memoirs of actual combat: breaking the border from webshell
猜你喜欢
![Speech synthesis: overview [generation task of unequal length sequence relation modeling]](/img/13/bd9def50f0efde49b622d139f63a83.png)
Speech synthesis: overview [generation task of unequal length sequence relation modeling]

Introduction to taro

Simple use of vlookup function in Excel -- exact matching or approximate matching data

How to recover data loss of USB flash disk memory card

表格背单词的方法

消息中间件:pulsar

【Redis】Redis6学习框架思路和细节

Wallpaper applet source code double ended wechat Tiktok applet

重磅发布 | 《FISCO BCOS应用落地指南》

语音信号处理-基础(一):声学基础知识
随机推荐
[hcie TAC] question 5-2
U盘内存卡数据丢失怎么恢复,这样操作也可以
Typescript variable declaration - type assertion
dcase_ Util tutorial
Blueprint basis
消息中间件:pulsar
Oracle subquery
Self attention mechanism
Mutex mutex
Standard | China payment and clearing Association releases the first privacy computing financial specification
324. 摆动排序 II / 剑指 Offer II 102. 加减的目标值
网上开股票账户真的安全吗?求答案
mysql 主键约束删除问题
Simple use of AWS elastic Beanstalk
Simple use of vlookup function in Excel -- exact matching or approximate matching data
Dialogue | prospects and challenges of privacy computing in the digital age
Chengtong network disk imitation blue playing network disk source code with video tutorial
Application of mediastreamer2 and GStreamer in embedded field
使用adb命令调试夜神模拟器
练习-选择排序