当前位置:网站首页>Three methods of finding inverse numbers
Three methods of finding inverse numbers
2022-07-01 23:52:00 【why151】
Violence law
#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;
}
Merge sort
#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;
}
Tree array method
#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;
}
边栏推荐
- 使用 pair 做 unordered_map 的键值
- . env. XXX file, with constant, but undefined
- 写给当前及未来博士研究生一些建议整理分享
- TS initial use, TS type
- How to realize parallel replication in MySQL replication
- mysql:insert ignore、insert和replace区别
- The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
- [understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
- Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
- ARP message header format and request flow
猜你喜欢

Is there a piece of code that makes you convinced by human wisdom

Material Design组件 - 使用BottomSheet展现扩展内容(一)

The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem

Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS

使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object

Redis AOF日志

Use pair to do unordered_ Key value of map

边缘计算概述

使用 pair 做 unordered_map 的键值

Chapter 6 data flow modeling
随机推荐
哈工大《信息内容安全》课程知识要点和难点
字典、哈希表、数组的概念
Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
2021 robocom world robot developer competition - semi finals of higher vocational group
Redis master-slave synchronization
Is it safe to buy funds on Great Wall Securities?
Concepts of dictionary, hash table and array
【QT】对于Qt MSVC 2017无法编译的问题解决
Is there a piece of code that makes you convinced by human wisdom
Why does blocprovider feel similar to provider?
Postgresql源码(57)HOT更新为什么性能差距那么大?
正则表达式收集
[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
cookie、session、tooken
使用 pair 做 unordered_map 的键值
Practical application and extension of plain framework
MySQL Replication中并行复制怎么实现
求逆序数的三个方法
Redis AOF log
起床困难综合症(按位贪心)