For a given sequence A={a0,a1,...an−1}A={a0,a1,...an−1}, the number of pairs (i,j)(i,j) where ai>ajai>aj and i<ji<j, is called the number of inversions. The number of inversions is equal to the number of swaps of Bubble Sort defined in the following program:

bubbleSort(A)
cnt = 0 // the number of inversions
for i = 0 to A.length-1
for j = A.length-1 downto i+1
if A[j] < A[j-1]
swap(A[j], A[j-1])
cnt++ return cnt

For the given sequence AA, print the number of inversions of AA. Note that you should not use the above program, which brings Time Limit Exceeded.

Input

In the first line, an integer nn, the number of elements in AA, is given. In the second line, the elements aiai (i=0,1,..n−1i=0,1,..n−1) are given separated by space characters.

output

Print the number of inversions in a line.

Constraints

  • 1≤n≤200,0001≤n≤200,000
  • 0≤ai≤1090≤ai≤109
  • aiai are all different

Sample Input 1

5
3 5 2 1 4

Sample Output 1

6

Sample Input 2

3
3 1 2

Sample Output 2

2
题意就是求一组数的逆序数,大小等于冒泡排序交换的次数,但看数据范围,这题用冒泡肯定超时。所以用归并排序模拟冒泡即可。
#include<iostream>
#include<cstring>
#include<stack>
#include<cstdio>
#include<cmath>
using namespace std;
#define MAX 500000
#define INF 2e9
int L[MAX/+],R[MAX/+];
long long cnt=;
long long merge(int A[],int n,int left,int mid,int right)
{
long long cnt=;
int n1=mid-left;
int n2=right-mid;
for(int i=;i<n1;i++)
{
L[i]=A[left+i];
}
for(int i=;i<n2;i++)
{
R[i]=A[mid+i];
}
L[n1]=INF;
R[n2]=INF;
int i=,j=;
for(int k=left;k<right;k++)//合并
{
if(L[i]<=R[j])
A[k]=L[i++];
else
{
A[k]=R[j++];
cnt=cnt+(n1-i);
}
}
return cnt;
}
long long mergeSort(int A[],int n,int left,int right)
{
long long v1,v2,v3;
if(left+<right)
{
int mid=(left+right)/;
v1=mergeSort(A,n,left,mid);
v2=mergeSort(A,n,mid,right);
v3=merge(A,n,left,mid,right);
return (v1+v2+v3);
}
else
return ;
}
int main()
{
int A[MAX],n;
cnt=;
cin>>n;
for(int i=;i<n;i++)
cin>>A[i];
cnt=mergeSort(A,n,,n);
cout<<cnt<<endl;
return ;
}

The Number of Inversions的更多相关文章

  1. [UCSD白板题] Number of Inversions

    Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...

  2. The Number of Inversions(逆序数)

    For a given sequence A={a0,a1,...an−1}A={a0,a1,...an−1}, the number of pairs (i,j)(i,j) where ai> ...

  3. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

  4. Inversions After Shuffle

    Inversions After Shuffle time limit per test 1 second memory limit per test 256 megabytes input stan ...

  5. Sequence Number

    1570: Sequence Number 时间限制: 1 Sec  内存限制: 1280 MB 题目描述 In Linear algebra, we have learned the definit ...

  6. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...

  7. HDU 6318 Swaps and Inversions 思路很巧妙!!!(转换为树状数组或者归并求解逆序数)

    Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. PAT 1009. Triple Inversions (35) 数状数组

    Given a list of N integers A1, A2, A3,...AN, there's a famous problem to count the number of inversi ...

  9. HDU 多校对抗赛第二场 1010 Swaps and Inversions

    Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

随机推荐

  1. 【python-leetcode206-翻转链表】反转链表

    问题描述: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL进阶:你可 ...

  2. 修改kali软件源并配置网络

    一.配置kali使用xshell连接 查看ssh服务状态 # service ssh status 开启ssh服务 # service ssh start 修改ssh配置文件 # vi /etc/ss ...

  3. python随用随学20200118-函数的高级特性

    高阶函数 话说当年C语言和Java里好像都有这么个东西...忘了 一句话说就是函数名本身就是一个引用. 可以作为变量传递. 一个简单的例子: def power_demo(x):  return x* ...

  4. JS水仙花数

    题目:3位数==个位立方+十位的立方+百位的立方.这个3位数就是水仙花数.要求打印出所有的水仙花数 <body> <div id=d1> </div> <sc ...

  5. 工作五年的.neter的一些经历感想和对未来的一些疑惑

    本次疫情在家办公快一个月了,节省了上下班的时间,外出活动时间,感觉有好多时间可以利用.人一闲下来就容易想事情,很多事情想不通心里堵的厉害,做事都提不起兴趣.至于想些什么呢,我给大家摆一下. 我的经历 ...

  6. [算法] Dijkstra算法(带权有向图 最短路径算法)

    一.带权有向图 二.算法原理 1)由于我们的节点是从1-6,所以我们创建的列表或数组都是n+1的长度,index=0的部分不使用,循环范围为1-6(方便计算). 2)循环之前,我们先初始化dis数组和 ...

  7. 使用 Apache James 3.3.0(开源免费) 搭建外网电子邮件服务器(基于 Windows + Amazon Corretto 8)

    对于邮件服务器的安装,请先参阅: 使用 Apache James 3.3.0(开源免费) 搭建内网电子邮件服务器(基于 Windows + Amazon Corretto 8) https://www ...

  8. P5727 【深基5.例3】冰雹猜想

    链接:Miku -------------------- 欸,为什么我第一遍没过 -------------------- #include<iostream> using namespa ...

  9. .net core 中如何运用 appsettings.json 进行配置开发、生产不同配置

    .net core 默认会有 appsettings.Development.json 文件,这是根据ASPNETCORE_ENVIRONMENT来读取的. 新建架构appsettings.Produ ...

  10. appium+python+unittest+HTMLRunner登录自动化测试报告

    环境搭建 python3Java JDK.netFrameworknodejsandroid SDKappiumAppium-Python-Client(pip install Appium-Pyth ...