利用归并排序统计逆序数,利用归并求逆序在对子序列s1和s2在归并时(s1,s2已经排好序),若s1[i]>s2[j](逆序状况),则逆序数加上s1.length-i,因为s1中i后面的数字对于s2[j]都是逆序的。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int N;
  4. int num[];
  5. int tmp[];
  6. __int64 count;
  7. void Merge(int l,int mid,int r){
  8. int i=l,j=mid+,k=;
  9. while(i<=mid&&j<=r){
  10. if(num[i]>num[j]) {
  11. tmp[k++]=num[j++];
  12. count+=mid-i+;
  13. } else
  14. {
  15. tmp[k++]=num[i++];
  16. }
  17. }
  18. while(i<=mid) tmp[k++]=num[i++];
  19. while(j<=r) tmp[k++]=num[j++];
  20.  
  21. for(i=; i<k; i++)
  22. {
  23. num[l+i]=tmp[i];
  24. }
  25. }
  26. void Mergesort(int l,int r){
  27. int mid=(l+r)/;
  28. if(l<r){
  29. Mergesort(l,mid);
  30. Mergesort(mid+,r);
  31. Merge(l,mid,r);
  32. }
  33. }
  34. int main(void) {
  35. scanf("%d",&N);
  36. int i=;
  37. while(N){
  38. for(i=;i<N;i++){
  39. scanf("%d",&num[i]);
  40. }
  41. count=;
  42. Mergesort(,N-);
  43. printf("%I64d \n",count);
  44. scanf("%d",&N);
  45. }
  46.  
  47. return EXIT_SUCCESS;
  48. }

附:

Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 48082   Accepted: 17536

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

  1. 5
  2. 9
  3. 1
  4. 0
  5. 5
  6. 4
  7. 3
  8. 1
  9. 2
  10. 3
  11. 0

Sample Output

  1. 6
  2. 0

Ultra-QuickSort - poj 2299 (归并排序+统计逆序数)的更多相关文章

  1. poj 2299 归并排序求逆序数 (可做模板)

    Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 48077   Accepted: 17533 Description In ...

  2. POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 70674   Accepted: 26538 ...

  3. POJ 2299 Ultra-QuickSort 求逆序数 线段树或树状数组 离散化

    我用的线段树写的. num数组表示已插入的数值的个数. 由于a[i]数值很大,但是n不是很大,所以要离散化处理 9 1 0 5 4 离散化后 4 1 0 3 2 这样保证最大值不会超过n #inclu ...

  4. poj 2299 Ultra-QuickSort 归并排序求逆序数对

    题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...

  5. poj 2299 Ultra-QuickSort :归并排序求逆序数

    点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34676   Accepted ...

  6. POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39279   Accepted: 14163 ...

  7. hiho一下 第三十九周 归并排序求逆序数

    题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...

  8. [CF 351B]Jeff and Furik[归并排序求逆序数]

    题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...

  9. POJ2299 Ultra-QuickSort(归并排序求逆序数)

    归并排序求逆序数   Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

随机推荐

  1. 【转载】linux2.6内核initrd机制解析

    题记 很久之前就分析过这部分内容,但是那个时候不够深入,姑且知道这么个东西存在,到底怎么用,来龙去脉咋回事就不知道了.前段时间工作上遇到了一个initrd的问题,没办法只能再去研究研究,还好,有点眉目 ...

  2. 数据库问题5-SYS.SYSPROCESSES使用和查找死锁

    http://blog.sina.com.cn/s/blog_62c4727d0100jc5z.html (一)理論部份 sys.sysprocesses (Transact-SQL) http:// ...

  3. Devexpress汉化修改 已经汉化过后生成的*.resources.dll文件 z

    Devexpress 是很好的第三方控件.但是需要汉化,在从网上下载的zh-CN文件夹里都是*.resources.dll文件,由于汉化的工作量很大,难免有些地方汉化 错误或者不合适(如下图).花了些 ...

  4. Jigsaw 项目:Java 模块系统新手引导

    前言 随着 2017 年 10 月 Java 9 的发布,Java 能够使用模块系统了,但是中文互联网上的资料太少,许多关于 Java 模块系统的文章都只是介绍了模块系统的好处,或者给了一些毫无组织的 ...

  5. nginx和selinux冲突

    cat /var/log/audit/audit.log |grep nginx |grep denied| audit2allow -M mynginx 取出selinux中有关于nginx被拒绝的 ...

  6. OpenGL实现多层绘制(Layered Rendering) [转]

    http://blog.csdn.net/u010462297/article/details/50589991 引言 在某些情况下会需要用到多层绘制.FBO下有多个颜色挂接点(Color Attac ...

  7. 转: ios学习入门进阶

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:利炳根链接:https://www.zhihu.com/question/19627420/answer/45962351来源 ...

  8. SEO优化100条

    1.准备个好域名.①.尽量在5位数内,当然也不一定,反正要让用户好记.(看个人):②.尽量用顶级的域名,搜索排名感觉好一点.③.做中文站最好用拼音注册,不要问为什么.看百度(baidu.com)就是很 ...

  9. mac 上多版本python 共存

    Mac上自带了Python2.x的版本,有时需要使用Python3.x版本做开发,但不能删了Python2.x,可能引起系统不稳定,那么就需要安装多个版本的Python. 1.安装Python3.x版 ...

  10. cocos2d-x OpenGL ES 坐标系总结

    很多教程都说cocos2d-x OpenGL ES世界坐标系原点在左下角,但至于为什么在左下角却从来没有人提过,这导致大部分人觉得这是OpenGL ES的规定,事实上这是错的.OpenGL ES的坐标 ...