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
已知逆序数等于冒泡排序的序列,但这题冒泡排序肯定超时。这题用归并排序优化一下就行。
AC代码
#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(逆序数)的更多相关文章
- HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description The inve ...
- HDU-Minimum Inversion Number(最小逆序数)
Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- 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 ...
- HDU 1394 Minimum Inversion Number(最小逆序数 线段树)
Minimum Inversion Number [题目链接]Minimum Inversion Number [题目类型]最小逆序数 线段树 &题意: 求一个数列经过n次变换得到的数列其中的 ...
- 逆序数2 HDOJ 1394 Minimum Inversion Number
题目传送门 /* 求逆序数的四种方法 */ /* 1. O(n^2) 暴力+递推 法:如果求出第一种情况的逆序列,其他的可以通过递推来搞出来,一开始是t[1],t[2],t[3]....t[N] 它的 ...
- HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- [HDU] 1394 Minimum Inversion Number [线段树求逆序数]
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
随机推荐
- Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树
Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树 Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序 ...
- 面试突击 | Redis 如何从海量数据中查询出某一个 Key?附视频
1 考察知识点 本题考察的知识点有以下几个: Keys 和 Scan 的区别 Keys 查询的缺点 Scan 如何使用? Scan 查询的特点 2 解答思路 Keys 查询存在的问题 Scan 的使用 ...
- C语言低级I/O(UNIX接口)
头文件说明 以下各函数均在<unistd.h>中 flags的各个值定义于<fcntl.h>中 BUFSIZ定义于<stdlib.h>中 (似乎<stdio. ...
- RxJava的concat操作符
更多文章请点击:http://77blogs.com/?p=170 转载请标明出处:https://www.cnblogs.com/tangZH/p/12088332.html,http://77bl ...
- Blazor初体验之寻找存储client-side jwt token的方法
https://www.cnblogs.com/chen8854/p/securing-your-blazor-apps-authentication-with-clientside-blazor-u ...
- vue-cli-service 报错
错误内容: vue-cli-service serve /bin/sh: vue-cli-service: command not found error Command failed with ex ...
- 使用JAVA导出EXCEL表格(POI)
一.POI概述 Jakarta POI 是一套用于访问微软格式文档的Java API.POI提供API给Java程序对Microsoft Office格式档案读和写的功能.在许多企业办公系统中,经常会 ...
- 本地服务开启MySQL57提示本地计算机上MySQL服务启动后停止。。。。
1.首先以管理员身份启动cmd,要不然服务禁止访问. 2.然后在cmd中输入 mysqld --remove mysql或者mysqld --remove mysql57来移除服务. 3.然后进入My ...
- MySQL基础篇(02):从五个维度出发,审视表结构设计
本文源码:GitHub·点这里 || GitEE·点这里 一.数据场景 1.表结构简介 任何工具类的东西都是为了解决某个场景下的问题,比如Redis缓存系统热点数据,ClickHouse解决海量数据的 ...
- linux--工具进阶
linux学习 看完了基础篇,下面来看进阶篇 我好想哭看这的时候,好多只是听说过,但完全没有试过,感觉自己懂得有点少,就是缺乏一些知识储备,也就是必须知道了某些或学过了某些知识才适合来看这一部分,看得 ...