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. 公司电脑安装mysql出现小问题

    按步骤将mysql安装好后,在自己电脑完全没问题,但是在公司电脑安装的时候出现了这样的问题. 查阅资料以后,找到了问题: 参考链接:https://blog.csdn.net/huacode/arti ...

  2. 手机monkey测试BUG重现及解决方法

    目录 1.1 Monkey测试简介...1 1.2 Monkey程序介绍...1 1.3 Monkey命令的简单帮助...2 1.4 Monkey命令参数介绍...2 1.5 Monkey测试步骤.. ...

  3. C89标准库函数手册

    http://zh.cppreference.com/w/c 前言 ANSI C(C89)标准库函数共有15个头文件.这15个头文件分别为: 1.<assert.h>            ...

  4. 【New Feature】阿里云快照服务技术解析

    一.背景   目前上云已经成为行业发展趋势,越来越多的企业级客户将业务系统和数据库迁移到云上.而传统的备份一体机/备份软件方式,并不适合云上ECS.RDS等产品的备份与容灾服务.阿里云块存储服务提供云 ...

  5. laravel 博客项目部署到Linux系统后报错 权限都设置为777,仍然报错没有权限

    阿里工程师最后给出解决方案.

  6. C++五:重载 多态

    C++五:重载与多态 一:概述   多态是指同样的消息被不同类型的对象接收导致不同的行为,即接口的多种不同的实现方式.多态可分为静态多态与动态多态.多态类型可分为四类:重载多态,强制多态,包含多态,参 ...

  7. SpringBoot @Transactional的rollbackFor属性

    1.简单回顾Java Exception 该图摘自:https://blog.csdn.net/zhangerqing/article/details/8248186 一方面,我们可以将异常分为运行时 ...

  8. @topcoder - TCO19 Regional Wildcard Wildcard Round - D1L2@ Diophantine

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 令 p[] 为质数序列:p[0] = 2, p[1] = 3, ...

  9. docker compose安装gitea

    docker-compose.yml version: "3.4" networks: gitea: external: false services: server: image ...

  10. LocalDate、LocalDateTime与timestamp、Date的转换

    LocalDate.LocalDateTime与timestamp.Date的转换 1.LocalDate转Date LocalDate nowLocalDate = LocalDate.now(); ...