当前位置:网站首页>2022河南萌新联赛第(三)场:河南大学 B - 逆序对计数
2022河南萌新联赛第(三)场:河南大学 B - 逆序对计数
2022-07-26 04:29:00 【WA_自动机】
B - 逆序对计数
解法:首先, 对于一个排列, 如果区间反转, 逆序数等于区间种所有的
对数减去当前的逆序对数, 即原本的正序对变为逆序对, 原本的逆
序对变为正序对。如果对于每个位置记录其到后面所有位置的逆
序对个数即可解决本题, 考虑从 [ l , r ] → [ l , r + 1 ] [l, r] → [l, r + 1] [l,r]→[l,r+1] 逆序对的个数增
加区间 [ l , r ] [l, r] [l,r] 中比 a r + 1 a_r+1 ar+1 的数的个数, 所以可以事前统计对于每一个
位置, 到他的前面的每一个位置比他大的数有多少个, 统计完直接
计算逆序对即可。
- 时间复杂度 O ( n 2 ) O(n^2) O(n2)
当然也可以使用树状数组统计逆序对, 可以很好的通过本题。
- 时间复杂度近似 O ( n 2 ) O(n^2) O(n2)
#include<cstdio>
#include<cstring>
const int N = 6010;
int a[N],tr[N],f[N][N],g[N][N];
int n;
inline int lowbit(int x)
{
return x&-x;
}
inline void add(int x,int y)
{
for(int i=x;i<=n;i+=lowbit(i))
tr[i]+=y;
}
inline int query(int x)
{
int res=0;
for(int i=x;i>0;i-=lowbit(i))
res+=tr[i];
return res;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
f[i][j]=f[i][j-1]+query(n)-query(a[j]-1);
g[i][j]=g[i][j-1]+query(a[j]);
add(a[j],1);
}
memset(tr,0,sizeof tr);
}
int q;scanf("%d",&q);
while(q--)
{
int l,r;scanf("%d%d",&l,&r);
printf("%d\n",f[1][n]-f[l][r]+g[l][r]);
}
return 0;
}
边栏推荐
- 机器学习之信用卡欺诈检测
- egg-ts-sequelize-CLI
- MapReduce中分区数与ReduceTask个数关系比较
- Analyzing the curriculum design evaluation system of steam Education
- Postman imports curl, exports curl, and exports corresponding language codes
- Recommendation Book Educational Psychology: a book for tomorrow's teachers~
- Optimization analysis and efficiency execution of MySQL
- Several methods of realizing high-low byte or high-low word exchange in TIA botu s7-1200
- Graph theory: topological sorting
- makefile知识再整理(超详细)
猜你喜欢
随机推荐
Yadi started to slow down after high-end
二、国际知名项目-HelloWorld
Acwing_ 12. Find a specific solution for the knapsack problem_ dp
生活相关——十年的职业历程(转)
Huawei executives talk about the 35 year old crisis. How can programmers overcome the worry of age?
Which websites can I visit to check the latest medical literature?
Sangi diagram of machine learning (for user behavior analysis)
解决:RuntimeError: Expected object of scalar type Int but got scalar type Double
1. Mx6u-alpha development board (main frequency and clock configuration experiment)
UE4 多个角色控制权的切换
Yuansaka Lin wallpaper
How does win11 set the theme color of the status bar? Win11 method of setting theme color of status bar
How to make your language academic when writing a thesis? Just remember four sentences!
ROS2的launch有何不同?
Matlab drawing
3、 @requestmapping annotation
Function knowledge points
AWS Support Plan
数组排序1
How to write abstract in English thesis?









