Description

  In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 
9 1 0 5 4 ,
Ultra-QuickSort produces the output 
0 1 4 5 9 .
  Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

  The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

  For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0
解题思路:
  本题揭示了排序的本质——消除逆序对。逆序对,是指对于满足 当i<j时,a[i]>a[j]的序偶(a[i],a[j])。可以证明,交换序列中任意两个
相邻元素,逆序对增加或减少一。那么对于一个给定的序列,按一次交换一对相邻元素的方法,最少需要要交换的次数等于此序列中逆序对的数目。
那么问题就变成了如何求给定序列的逆序对。可以采用分治的方法:
  整个序列逆序对数=左半序列逆序对数+右半序列逆序对数+前一个元素在左半序列,后一个元素在右半序列的逆序对数。
  再细考虑可以发现,这个过程可以在归并排序的同时完成,时间复杂度为O(NlogN). 注意:可以简单计算一下,n个元素的排列逆序对数最多有 n(n-1)/2个,需要用到long long。 代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
using namespace std;
#define print_time_ printf("time : %f\n",double(clock())/CLOCKS_PER_SEC)
#define maxn 500000
int A[maxn+]; typedef long long LL;
LL DC(int a,int N){
int mid=a+N/-;
int b=a+N-;
if(N==)
return ; LL x1=DC(a, N/);
LL x2=DC(mid+,b-mid);
LL x3=;
int *B=new int[N];
int i=a,j=mid+,p=;
for(;i<=mid&&j<=b&&p<N;p++){
if(A[i]<=A[j]){
B[p]=A[i++];
}
else {
B[p]=A[j++];
x3+=mid-i+;
}
}
while(i<=mid){B[p++]=A[i++];}
while(j<=b){B[p++]=A[j++];}
memcpy(A+a, B, N*sizeof(int));
delete [] B;
return x1+x2+x3;
}
int main() {
int n;
while(scanf("%d",&n)==&&n){
for(int i=;i<n;i++)
scanf("%d",&A[i]);
printf("%lld\n",DC(, n));
}
//print_time_;
return ;
}



Ultra-QuickSort——[归并排序、分治求逆序对]的更多相关文章

  1. AC日记——codevs 1688 求逆序对

    1688 求逆序对  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 给定一个序列a1,a2,…, ...

  2. 【BZOJ4769】超级贞鱼 归并排序求逆序对

    [BZOJ4769]超级贞鱼 Description 马达加斯加贞鱼是一种神奇的双脚贞鱼,它们把自己的智慧写在脚上——每只贞鱼的左脚和右脚上各有一个数.有一天,K只贞鱼兴致来潮,排成一列,从左到右第i ...

  3. poj2299(归并排序求逆序对)

    题目链接:https://vjudge.net/problem/POJ-2299 题意:给定一个序列,每次只能交换邻近的两个元素,问要交换多少次才能使序列按升序排列. 思路:本质就是求逆序对.我们用归 ...

  4. 归并排序+归并排序求逆序对(例题P1908)

    归并排序(merge sort) 顾名思义,这是一种排序算法,时间复杂度为O(nlogn),时间复杂度上和快排一样 归并排序是分治思想的应用,我们先将n个数不断地二分,最后得到n个长度为1的区间,显然 ...

  5. HDU 3743 Frosh Week(归并排序求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3743 题目意思就是给你一个长为n的序列,让你求逆序对.我用的是归并排序来求的.归并排序有一个合并的过程 ...

  6. 浙江工商大学15年校赛I题 Inversion 【归并排序求逆序对】

    Inversion Time Limit 1s Memory Limit 131072KB Judge Program Standard Ratio(Solve/Submit) 15.00%(3/20 ...

  7. 2014 HDU多校弟五场A题 【归并排序求逆序对】

    这题是2Y,第一次WA贡献给了没有long long 的答案QAQ 题意不难理解,解题方法不难. 先用归并排序求出原串中逆序对的个数然后拿来减去k即可,如果答案小于0,则取0 学习了归并排序求逆序对的 ...

  8. Day2:T4求逆序对(树状数组+归并排序)

    T4: 求逆序对 A[I]为前缀和 推导 (A[J]-A[I])/(J-I)>=M A[j]-A[I]>=M(J-I) A[J]-M*J>=A[I]-M*I 设B[]=A[]-M*( ...

  9. 归并排序&&归并排序求逆序对

    归并排序 归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序 ...

随机推荐

  1. Leetcode908.Smallest Range I最小差值1

    给定一个整数数组 A,对于每个整数 A[i],我们可以选择任意 x 满足 -K <= x <= K,并将 x 加到 A[i] 中. 在此过程之后,我们得到一些数组 B. 返回 B 的最大值 ...

  2. 使用HashMap编写一程序实现存储某班级学生信息

    1. 使用HashMap编写一程序实现存储某班级学生信息,要求在屏幕上打印如下列表 学号   姓名   性别   年龄 001    张三   男      23 002    李四   男      ...

  3. golang数据类型二

    字符类型 3.14基本数据类型的相互转换 3.15基本数据类型和string的转换 FormatInt // FormatUint 将 int 型整数 i 转换为字符串形式// base:进位制(2 ...

  4. JS 寄生 继承

    寄生构造函数 寄生构造函数的用途目的:给String内置对象功能扩充 稳妥的构造函数 继承 对象冒充继承 一般继承 组合继承 原型链继承:借助于中转函数和已有对象 寄生式继承:把原型式+工厂式结合而来 ...

  5. python 在init方法中导入数据

  6. JavaScript--函数中()的作用

    在函数中参数是函数的时候:function a(函数名) 与 function a(函数名()) 的区别: // 在函数里面() 是一个编组和立即执行的功能 /** * function autoPl ...

  7. git 密钥

    为什么配置SHH呢?是为了方便我们剪切代码的时间免密码输入,特别方便如何配置呢? 首先安装git: 先到官网下载:官网下载git 然后安装后在桌面任意空白处右击,选择Git Base Here即可如下 ...

  8. Python学习之路15☞socket编程

    一 客户端/服务器架构 即C/S架构,包括 1.硬件C/S架构(打印机) 2.软件C/S架构(web服务) C/S架构与socket的关系: 我们学习socket就是为了完成C/S架构的开发 二 os ...

  9. 页面头部<meta>中的属性和含义

    1<meta name="robots" content="index, follow" />    none:搜索引擎将忽略此网页,等价于noin ...

  10. Redis源码解析:08对象

    前面介绍了Redis用到的所有主要数据结构,比如简单动态字符串(SDS).双端链表.字典.压缩列表.整数集合等.然而Redis并没有直接使用这些数据结构来实现键值对数据库,而是基于这些数据结构创建了一 ...