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 此题就是要求逆序数,所以用归本排序较好。
#include<iostream>

using namespace std;

long long  cnt;

void merge(int array[],int left,int mid,int right)
{
int* temp=new int[right-left+1];
int i,j,p;
for(i=left,j=mid+1,p=0;i<=mid&&j<=right;p++)
{
if(array[i]<=array[j])temp[p]=array[i++];
else
{
temp[p]=array[j++];cnt+=(mid-i+1);
}
}
while(i<=mid)temp[p++]=array[i++];
while(j<=right)temp[p++]=array[j++];
for(i=left,p=0;i<=right;i++)array[i]=temp[p++];
delete temp;
} void mergesort(int array[],int left,int right)
{
if(left==right)array[left]=array[right];
else
{
int mid=(left+right)/2;
mergesort(array,left,mid);
mergesort(array,mid+1,right);
merge(array,left,mid,right);
}
}
int main()
{
int n,array[500005];
while(cin>>n,n)
{
cnt=0;
for(int i=0;i<n;i++)
cin>>array[i];
mergesort(array,0,n-1);
cout<<cnt<<endl;
}
return 0;
}

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. HttpURLConnection请求接口

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataOutputStream; ...

  2. UEditor 之查询当前编辑区域的状态是源码模式还是可视化模式

    在使用百度的编辑器的时候,遇到了这样的一个问题: 解决方法是 使用了两个命令:

  3. Hbase之进行批处理操作

    import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...

  4. SCCM日志存放路径

    sccm有一些比较重要的日志大家可能经常需要查看.下面是一些比较常用的日志文件的存放位置. 客户端日志文件: 日志位于服务器端 SMS_CCM\Logs 文件夹中 日志位于客户端 %Windir%\S ...

  5. Tiny PXE Server简介

    Tiny PXE Server简介Tiny PXE Server是一款小巧而功能强大的网启软件.支持DHCP TFTP HTTP BINL DNS等多个协议,支持grub4dos,pxelinux,i ...

  6. JQUERY学习(壹)

    一.jQuery的引言 1.jQuery框架:对JavaScript的封装,简化js开发 2.jQuery框架的好处: 1)语法简单 js中:document.getElementById(" ...

  7. js高级程序设计(三)基本概念

    数据类型 ECMAscript中有五种简单数据类型Undefined,Null,Boolean,Number,String 还有一种复杂数据类型Object. typeof操作符 typeof可能返回 ...

  8. java 三大框架 介绍

    三大框架:Struts+Hibernate+Spring Java三大框架主要用来做WEN应用. Struts主要负责表示层的显示 Spring利用它的IOC和AOP来处理控制业务(负责对数据库的操作 ...

  9. hdu-----(2807)The Shortest Path(矩阵+Floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  10. case when 对某个字段值分类讨论

    SELECT SM_ID,SM_CID,SM_STATION,SM_TIME,PS_CODE,PS_NUMBER,SS_NAME,SS_CODE, ( THEN '中转站' END) FROM dbo ...