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. 简析 addToBackStack使用和Fragment执行流程

    在使用Fragment的时候我们一般会这样写: FragmentTransaction transaction = getSupportFragmentManager().beginTransacti ...

  2. 每日一笔记之3:QTconnect()

    刚学习QT的时候,跟着教程做一些简答的实验,教程简单的界面使用UI文件,直接在界面上拖一个按键,在右键go to slot,在编写槽函数. 我以前没学过C++,一直以为这个自动跳转过去的slot函数是 ...

  3. hiho1091_clicker背包问题

    问题 类似有限背包问题,题目链接:clicker 实现 #include<stdio.h> #include<cmath> #include<iostream> # ...

  4. svn 合并分支 等

    [转载]svn分支(branch)创建.合并(到trunk).冲突解决. Leave a reply 转载自:http://zccst.iteye.com/blog/1430823 一.创建分支 1, ...

  5. Convention插件 struts零配置

    http://blog.csdn.net/spyjava/article/details/13631961系列课程使用 注解:http://www.yiibai.com/struts_2/struts ...

  6. STM8s在利用库配置端口的小问题

    在应用的时候PA2口需要设置成推挽输出,控制一个外部电源开关,端口初始化程序如下: GPIO_DeInit(GPIOA); GPIO_Init(GPIOA,GPIO_PIN_2,GPIO_MODE_O ...

  7. html list <==> unformatted list

    两者的区别很大不得不擦呢,任何两个东西的差别都很大,不得混淆.---一个有ul/ol的选择,一个没有

  8. 集成代码编辑器ACE的经验

    ACE是最流行的在线代码编辑器之一,在CanTK的集成开发环境GameBuilder里也使用了ACE.ACE的功能非常强大,但是由于使用方法不当,大家反映GameBuilder的代码编辑器不好用.最近 ...

  9. Computer assembly and maintenance

    转载请注明出处: 我所遇见的世界会更美好 第一章 计算机的基本构成和组装 1,内存的组成? (1)  RAM(随机存取存储器) (2)  ROM(只读存储器) (3)  Cache(高速缓存) 2,S ...

  10. Android要点记录

    1.Toolbar不能上滑隐藏的原因<!--如果在CoordinatorLayout加上android:fitsSystemWindows="true",而不是在AppLay ...