当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
![[must] bm41 output the right view of the binary tree [medium +]](/img/a5/00b2f0df5ab448665a2b062d145e52.png)
[must] bm41 output the right view of the binary tree [medium +]

使用VB.net将PNG图片转成icon类型图标文件

Overview of edge calculation

Concepts of dictionary, hash table and array

Pytorch learning record

Practical application and extension of plain framework

Use vb Net to convert PNG pictures into icon type icon files

. env. XXX file, with constant, but undefined

深度学习 | 三个概念:Epoch, Batch, Iteration

门级建模—课后习题
随机推荐
ADO. Net SqlConnection object usage summary
mysql:insert ignore、insert和replace区别
[es practice] safe operation mode on ES
Oracle中已定义者身份执行函数AUTHID DEFINER与Postgresql行为的异同
How to display real-time 2D map after rviz is opened
2021 robocom world robot developer competition - semi finals of higher vocational group
Material design component - use bottomsheet to show extended content (I)
有没有一段代码,让你为人类的智慧所折服
openwrt 开启KV漫游
excel如何打开100万行以上的csv文件
Regular expression collection
Learn online case practice
求逆序数的三个方法
字典、哈希表、数组的概念
cookie、session、tooken
Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
Which securities company is the best to open a stock account? Is there a security guarantee
哈工大《信息内容安全》课程知识要点和难点
MySQL Replication中并行复制怎么实现
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee