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>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的更多相关文章
- [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 ...
- 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> ...
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- Inversions After Shuffle
Inversions After Shuffle time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Sequence Number
1570: Sequence Number 时间限制: 1 Sec 内存限制: 1280 MB 题目描述 In Linear algebra, we have learned the definit ...
- HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...
- HDU 6318 Swaps and Inversions 思路很巧妙!!!(转换为树状数组或者归并求解逆序数)
Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 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 ...
- HDU 多校对抗赛第二场 1010 Swaps and Inversions
Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
随机推荐
- STT-MRAM万能存储器芯片
传统存储器的技术局限以及不断缩小的制造尺寸所带来的巨大挑战促使科研人员开始寻找新一代存储器件,它应具有接近静态存储器的纳秒级读写速度,具有动态存储器甚至闪存级别的集成密度和类似Flash的非易失性存储 ...
- 邓Laravel2020-01-28
ORM .一对一 hasOne $this->hasOne('外键的模型名称','外键的id','自己表里对应的id')// demo一对一 用户表里又area_id与地区表id一致 用户表和地 ...
- Docker下Jenkins的安装部署、更新
一.下载Jenkins镜像 docker pull jenkins/jenkins 二.创建挂载文件 mkdir /srv/jenkins chown -R : /srv/jenkins 三.启动Do ...
- Asp.Net Core 3.1 集成Swagger
引入Nuget包 Swashbuckle.AspNetCore.SwaggerGen Swashbuckle.AspNetCore.SwaggerUI 配置Startup 配置ConfigureSer ...
- 第7章.字符串、String类和StringBuilder类
参考链接: https://www.runoob.com/csharp/csharp-string.html https://www.cnblogs.com/cang12138/p/7323709.h ...
- phpcms搜索给分页代码添加参数方法
在使用PHPCMS开发网站时遇到了一个搜索功能,需要在搜索结果分页链接上添加一些传递的参数,不然不能够正确翻页. 方法: 找到\phpcms\libs\functions\global.func.ph ...
- Tomcat + mysql + myeclipse 启动遇到的问题
1. 问题: Tomcat启动时报错如下:Table 'performance_schema.session_variables' doesn't exist 2. 网络上普遍找到的解决办法: 控制台 ...
- C语言->关于文件数据的录入和输出调用的函数总结
数据输入输出对象之间的关系图: 函数使用说明: 1.一个字符的输入\输出,对象是键盘(缓存和屏幕) 1.1.getchar(a),putchar(a); 1.2.scanf(“%d”,&i), ...
- 嵊州D5T3 指令 program 神奇的位运算
指令 program [问题描述] krydom 有一个神奇的机器. 一开始,可以往机器里输入若干条指令: opt x 其中,opt 是 & | ^ 中的一种,0 ≤ x ≤ 1023 . 对 ...
- 虚拟机NAT模式连接外网
虚拟机三种联网方式: 一.NAT(推荐使用) 功能:①可以和外部网络连通 ②可以隔离外部网络 二.桥接模式 功能:直接 ...