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. Mac启动MongoDB报错:exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating

    这是主要错误: initAndListen中的异常:NonExistentPath:找不到数据目录/ data / db. 最新版的Mac系统Catalina发生了令人惊讶的更改:它不允许更改根目录( ...

  2. C#模拟POST上传文件帮助类(支持https、http)

    public static int PostFile(string getUrl, CookieContainer cookieContainer, HttpHeader header, string ...

  3. ES6 - 基础学习(2): 新的变量声明方式 let 与 const

    ES6)新增加了两个重要的 JavaScript 关键字:let 和 const.以前声明变量时只有一种方式:var,ES6对声明方式进行了扩展,现在可以有三种声明方式了. 1.var:variabl ...

  4. 检测APK是否存在Janus漏洞步骤

    Janus说明   Android APP仅使用V1签名,可能存在Janus漏洞(CVE-2017-13156),Janus漏洞(CVE-2017-13156)允许攻击者在不改变原签名的情况下任意修改 ...

  5. 03.JS运算符

    前言:   学习一门编程语言的基本步骤 (01)了解背景知识 (02)搭建开发环境 (03)语法规范 (04)常量和变量 (05)数据类型 (06)数据类型转换 (07)运算符7.运算符 表达式:由运 ...

  6. 重构与动态为angularjs栏位赋值或获取值

    先来看下面一段html: 这个ng-model名称带有一定的规律带有序号. 先来实现数据绑定,从数据取到数据后,为ng-model绑定相对应的值: var c = response.data $sco ...

  7. H5Demo_password_generator

    原项目资源地址: https://www.html5tricks.com/js-passwd-generator.html codepen地址: https://codepen.io/deuscx/p ...

  8. textarea输入文字限制个数

    说明: w-count固定为数字部分的class textarea-active为超出最大输入文字个数报错信息的class html 部分: <div class="wrap wrap ...

  9. SqlServer 常用语句方法

    批量生成删表语句 select 'drop table '+b.name+'.'+a.name+';' from sys.tables a left join sys.schemas b on a.s ...

  10. ssh 或 putty 连接linux报错解决方法

    由于当天多次输入错误密码,ssh和putty就连接不上了,纠结了很久解决问题 ssh连接提示错误:server unexpectedly closed network connection putty ...