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. 【风马一族_php】数组函数

    原文来自:http://www.cnblogs.com/sows/p/6045699.html (博客园的)风马一族 侵犯版本,后果自负  2016-11-09 15:56:26 数组 函数 php- ...

  2. log4j:ERROR Could not read configuration file [log4j.properties]

    遇到这个错误,程序能够正常运行,log4j.properties也在classpath中,后来在网上查了资料,把下面这个语句去掉就好啦. PropertyConfigurator.configure( ...

  3. 使用JS如何消除一个数组里重复的元素

    JS: var arrData = [1,3,5,7,7,8,9,3,10,8,"sdsdsds","sss","ffff","s ...

  4. get请求url中带有中文参数出现乱码情况

    在项目中经常会遇到中文传参数,在后台接收到乱码问题.那么在遇到这种情况下我们应该怎么进行处理让我们传到后台接收到的参数不是乱码是我们想要接收的到的,下面就是我的一些认识和理解. get请求url中带有 ...

  5. 免费报名 | 汇聚HBase&大数据最前沿 Apache HBaseConAsia2019盛会火热来袭

    Apache HBase介绍 Apache HBase是基于Apache Hadoop构建的一个高可靠性.高性能.可伸缩的分布式存储系统,它提供了大数据背景下的高性能的随机读写能力,HBase是Goo ...

  6. (五)IO流之ByteArrayInput/OutputStream

    ByteArrayInputStream:是把字节数组当成源的输入流 String string="hello shanghai"; ByteArrayInputStream bi ...

  7. 登录注册beta版

    注册 login_count = 0 username_inp = input('请输入用户名:') while login_count < 3: pwd_inp = input('请输入密码: ...

  8. @bzoj - 4298@ [ONTAK2015]Bajtocja

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定d张无向图,每张图都有n个点.一开始,在任何一张图中都没有任 ...

  9. 【算法】[leetcode] permutations的讨论(转载)

    原题是找到一组数的全排列 Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  10. 巨蟒python全栈开发-第11阶段 ansible_project1

    今日大纲: 1.前端页面介绍 2.发布流程 3.需求分析 4.表结构设计 5.前端页面设计 昨日内容回顾: 1.roles - tasks - handlers - files - templates ...