当前位置:网站首页>求逆序数的三个方法
求逆序数的三个方法
2022-07-01 23:49:00 【why151】
暴力法
#include<iostream>
#include<algorithm>
using namespace std;
int a[2010];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
if(a[i]>a[j]) ans+=1;
}
cout<<ans<<endl;
return 0;
}
归并排序法
#include<iostream>
#include<algorithm>
using namespace std;
int a[2010],q[2010];
int ans=0;
void arrange_sort(int l,int r)
{
if(l>=r) return;
int mid=l+r>>1;
arrange_sort(l,mid);
arrange_sort(mid+1,r);
int k=0;
int i=l,j=mid+1;
while(i<=mid&&j<=r)
{
// cout<<i<<' '<<j<<' '<<l<<' '<<r<<' '<<a[i]<<' '<<a[j]<<endl;
if(a[i]<=a[j]) q[++k]=a[i++];
else
{
q[++k]=a[j++];
ans+=mid-i+1;
}
}
while(i<=mid) q[++k]=a[i++];
while(j<=r) q[++k]=a[j++];
for(int i=l,j=1;j<=k;j++,i++)
a[i]=q[j];
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
arrange_sort(1,n);
cout<<ans<<endl;
}
树状数组法
#include<algorithm>
#include<iostream>
#include<unordered_map>
using namespace std;
unordered_map<int,int> a;
int lowbit(int x)
{
return x&-x;
}
void insert(int x,int k)
{
while(x<=0x3f3f3f3f)
{
a[x]+=k;
x+=lowbit(x);
}
}
int query(int x)
{
int ans=0;
while(x)
{
ans+=a[x];
x-=lowbit(x);
}
return ans;
}
int main()
{
int n;
cin>>n;
int ans=0;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
insert(x,1);
ans+=i-query(x);
}
cout<<ans<<endl;
}
边栏推荐
- How to display real-time 2D map after rviz is opened
- What professional classification does the application of Internet of things technology belong to
- Postgresql随手记(10)动态执行EXECUTING语法解析过程
- Use vb Net to convert PNG pictures into icon type icon files
- [es practice] safe operation mode on ES
- Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four
- PyTorch学习记录
- SQL optimization
- Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
- Know --matplotlib
猜你喜欢

Notblank and notempty
![[es practice] safe operation mode on ES](/img/3f/fa28783770098ff10bffeccd64fe51.png)
[es practice] safe operation mode on ES

边缘计算概述

学成在线案例实战

Current situation and future development trend of Internet of things

E-commerce RPA robot helps brand e-commerce to achieve high traffic

Deep learning | three concepts: epoch, batch, iteration
![[QT] solve the problem that QT MSVC 2017 cannot compile](/img/35/e458fd437a0bed4bace2d6d65c9ec8.png)
[QT] solve the problem that QT MSVC 2017 cannot compile

2021 RoboCom 世界机器人开发者大赛-高职组复赛

Multi table operation - one to one, one to many and many to many
随机推荐
边缘计算概述
PyTorch学习记录
vs2015 AdminDeployment.xml
【C#】依赖注入及Autofac
cookie、session、tooken
RPA教程01:EXCEL自动化从入门到实操
Algolia's search needs are almost closed
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
2021 RoboCom 世界机器人开发者大赛-本科组初赛
algolia 搜索需求,做的快自闭了...
The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
Li Kou today's question -241 Design priorities for operational expressions
Which securities company is the best to open a stock account? Is there a security guarantee
Why is PHP called hypertext preprocessor
[untitled]
北京炒股开户选择手机办理安全吗?
[leetcode] length of the last word [58]
2021 robocom world robot developer competition - semi finals of higher vocational group
华为HMS Core携手超图为三维GIS注入新动能