当前位置:网站首页>求逆序数的三个方法
求逆序数的三个方法
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;
}
边栏推荐
- .env.xxx 文件,加了常量,却undefined
- 华为HMS Core携手超图为三维GIS注入新动能
- Notblank and notempty
- Oracle中已定义者身份执行函数AUTHID DEFINER与Postgresql行为的异同
- 使用 pair 做 unordered_map 的键值
- 2021 robocom world robot developer competition - preliminary competition of higher vocational group
- Redis AOF日志
- [must] bm41 output the right view of the binary tree [medium +]
- Use pair to do unordered_ Key value of map
- TS initial use, TS type
猜你喜欢

Concurrentskiplistmap -- principle of table skipping

Current situation and future development trend of Internet of things

2021 RoboCom 世界机器人开发者大赛-本科组初赛

PostgreSQL source code (57) why is the performance gap so large in hot update?

Practical application and extension of plain framework

Redis master-slave synchronization

from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘

ARP message header format and request flow

RPA教程01:EXCEL自动化从入门到实操

字典、哈希表、数组的概念
随机推荐
华为HMS Core携手超图为三维GIS注入新动能
2021 RoboCom 世界机器人开发者大赛-高职组复赛
Zero foundation tutorial of Internet of things development
Using SqlCommand objects in code
深度学习 | 三个概念:Epoch, Batch, Iteration
Resumption of attack and defense drill
ADO. Net SqlConnection object usage summary
Depth first search and breadth first search of graph traversal
Openwrt enable kV roaming
Overview of edge calculation
【ES实战】ES上的安全性运行方式
Windows 7 安装MYSQL 错误:1067
. env. XXX file, with constant, but undefined
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
Which securities company is the best to open a stock account? Is there a security guarantee
【QT】测试Qt是否能连接上数据库
在长城证券上买基金安全吗?
What category does the Internet of things application technology major belong to
Oracle中已定义者身份执行函数AUTHID DEFINER与Postgresql行为的异同
The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem