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
  3.  
  4. 此题就是要求逆序数,所以用归本排序较好。
  5.  
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. long long cnt;
  6.  
  7. void merge(int array[],int left,int mid,int right)
  8. {
  9. int* temp=new int[right-left+1];
  10. int i,j,p;
  11. for(i=left,j=mid+1,p=0;i<=mid&&j<=right;p++)
  12. {
  13. if(array[i]<=array[j])temp[p]=array[i++];
  14. else
  15. {
  16. temp[p]=array[j++];cnt+=(mid-i+1);
  17. }
  18. }
  19. while(i<=mid)temp[p++]=array[i++];
  20. while(j<=right)temp[p++]=array[j++];
  21. for(i=left,p=0;i<=right;i++)array[i]=temp[p++];
  22. delete temp;
  23. }
  24.  
  25. void mergesort(int array[],int left,int right)
  26. {
  27. if(left==right)array[left]=array[right];
  28. else
  29. {
  30. int mid=(left+right)/2;
  31. mergesort(array,left,mid);
  32. mergesort(array,mid+1,right);
  33. merge(array,left,mid,right);
  34. }
  35. }
  36. int main()
  37. {
  38. int n,array[500005];
  39. while(cin>>n,n)
  40. {
  41. cnt=0;
  42. for(int i=0;i<n;i++)
  43. cin>>array[i];
  44. mergesort(array,0,n-1);
  45. cout<<cnt<<endl;
  46. }
  47. return 0;
  48. }

Program A-归并排序的更多相关文章

  1. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  2. Ultra-QuickSort【归并排序典型题目】

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34470   Accepted: 12382 ...

  3. poj 1804 (nyoj 117)Brainman : 归并排序求逆序数

    点击打开链接 Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7810   Accepted: 4261 D ...

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

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

  5. 八大排序方法汇总(选择排序,插入排序-简单插入排序、shell排序,交换排序-冒泡排序、快速排序、堆排序,归并排序,计数排序)

    2013-08-22 14:55:33 八大排序方法汇总(选择排序-简单选择排序.堆排序,插入排序-简单插入排序.shell排序,交换排序-冒泡排序.快速排序,归并排序,计数排序). 插入排序还可以和 ...

  6. 高效算法——A 归并排序

    In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...

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

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

  8. Ultra-QuickSort(归并排序求逆序对数)

    Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34283   Accepted: 12295 Description In ...

  9. HDU1394 Minimum Inversion Number(线段树OR归并排序)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  10. A.归并排序

    归并排序 (求逆序数) 归并排序:递归+合并+排序 时间复杂度:O(n logn)    空间复杂度:O(n) 用途:1.排序  2.求逆序对数 Description In this problem ...

随机推荐

  1. VI查找与替换

    一.vi查找:    当你用vi打开一个文件后,因为文件太长,如何才能找到你所要查找的关键字呢?在vi里可没有菜单-〉查找, 不过没关系,你在命令模式下敲斜杆(/)这时在状态栏(也就是屏幕左下脚)就出 ...

  2. drupal里面的ajax最粗浅的理解-流程

    1,  form里面的ajax所在地表单元素有一个事件,激发system/ajax,相应的有ajax_form_callback(), 会把被改变的元素值传到form_state[values]中, ...

  3. VC++多工程项目

    目录 第1章 VC++6.0    1 1.1 设置依赖关系    1 1.2 编译顺序    2 1.3 自动连接    3 1.4 静态库    3 1.4.1 嵌入    3 1.4.2 替换  ...

  4. oncontextmenu

    一个页面中,BODY中用oncontextmenu='return false'来取消鼠标右键:在JS中设置oncontextmenu='return true'用window.document.on ...

  5. 创建高性能移动 web 站点

    如果你的网站3秒钟没有响应,人们就会失去兴趣了.为了满足响应快这个愿望,需要一个不同的方法在手机上进行分析,设计和测试. 这篇文章将会对Johan Johansson在2013年4月提出" ...

  6. python中的popen和subprocess

    import os from subprocess import Popen, PIPE res = os.popen('xx.exe E:\\test\\file1 E:\\test\\file2' ...

  7. JQ添加移除css样式

    1. addClass() - 添加CSS类 $("#target").addClass("newClass"); //#target 指的是需要添加样式的元素 ...

  8. PowerDesigner中导出设计说明文档

    点击下图的新建按钮,新建一个导出内容的模板 模板设计界面分为2栏,左边是可选的模板内容,右侧是模板,双击左侧条目会添加到右侧,最后生成的文件中就有此项内容. 已经添加到右侧的内容可以编辑,双击右侧的条 ...

  9. Swing——JFrame

    1.定义 相对于AWT(hevay weight component),Swing(hevay weight component)是轻量化的组件.Swing由纯Java Code 所写,解决了Java ...

  10. jQuery制作瀑布流(转)

    “瀑布流布局”随着pinterest网的流行而出名,现在国内使用这种风格布局的网站也越来越多,比如说Mark之,蘑菇街,点点网,哇哦等等.我第一次听到这个布局名称是来自于“乔花写的<瀑布流布局浅 ...