当前位置:网站首页>求逆序数的三个方法
求逆序数的三个方法
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;
}
边栏推荐
- RPA教程01:EXCEL自动化从入门到实操
- Write some suggestions to current and future doctoral students to sort out and share
- Deep learning | three concepts: epoch, batch, iteration
- What are the common types of points mall games?
- Pytorch learning record
- 边缘计算概述
- Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
- [QT] qtcreator uninstall and installation (abnormal state)
- What professional classification does the application of Internet of things technology belong to
- Why is PHP called hypertext preprocessor
猜你喜欢
ConcurrentSkipListMap——跳表原理
How to solve the image pop-up problem when pycharm calls Matplotlib to draw
【QT】测试Qt是否能连接上数据库
Current situation and future development trend of Internet of things
De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
What category does the Internet of things application technology major belong to
Relatively easy to understand PID understanding
Redis AOF log
Door level modeling - after class exercises
Use vb Net to convert PNG pictures into icon type icon files
随机推荐
Future trend and development of neural network Internet of things
Matplotlib common charts
[QT] qtcreator uninstall and installation (abnormal state)
图的遍历之深度优先搜索和广度优先搜索
学成在线案例实战
Redis 主从同步
Oracle中已定义者身份执行函数AUTHID DEFINER与Postgresql行为的异同
Notblank and notempty
Material design component - use bottomsheet to show extended content (I)
MySQL: the difference between insert ignore, insert and replace
[must] bm41 output the right view of the binary tree [medium +]
【QT】QtCreator卸载与安装(非正常状态)
ADO. Net SqlDataAdapter object
【QT】對於Qt MSVC 2017無法編譯的問題解决
Redis AOF日志
电商RPA机器人,助力品牌电商抢立流量高点
2021 RoboCom 世界机器人开发者大赛-高职组初赛
Know --matplotlib
Development trend and future direction of neural network Internet of things
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘