Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 44489   Accepted: 16176

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

题目大意就是让你计算一个冒泡排序中,需要交换的次数。
以为数据量较大,所以我们这里用到了Merge Sort :
 #include<stdio.h>
#include<string.h>
int temp[] ;
int a[] ;
__int64 number ;
void MergeSort( int a[] , int fir , int end )
{
int len = end - fir ;
if( len <= )
return ;
int mid = fir + len/ ;
MergeSort( a , fir , mid ) ;
MergeSort( a , mid , end ) ;
int p1 = fir , p2 = mid ;
for( int i = fir ; i < end ; i++ )
{
if( p1 == mid )
{
temp[i] = a[p2++] ;
}
else if( p2 == end )
{
temp[i] = a[p1++] ;
}
else
{
if( a[p1] >= a[p2] )
{
temp[i] = a[p2++] ;
number += mid - p1 ;//在Merge Sort 排序中仅仅多加了这句话
}
else
{
temp[i] = a[p1++] ;
}
}
} for(int i = fir ; i < end ; i++ )
{
a[i] = temp[i] ;
}
}
int main()
{
// freopen("a.txt" ,"r" , stdin );
int n ;
while( scanf("%d" , &n ) != EOF )
{
if( n == ) break;
number = ;
for(int i = ; i < n ; i++ )
scanf("%d" , &a[i] ) ;
MergeSort( a , , n ) ; printf("%I64d\n" , number );
}
return ;
}

AC

Ultra-QuickSort的更多相关文章

  1. quickSort算法导论版实现

    本文主要实践一下算法导论上的快排算法,活动活动. 伪代码图来源于 http://www.cnblogs.com/dongkuo/p/4827281.html // imp the quicksort ...

  2. Javascript算法系列之快速排序(Quicksort)

    原文出自: http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/ https://gis ...

  3. JavaScript 快速排序(Quicksort)

    "快速排序"的思想很简单,整个排序过程只需要三步: (1)在数据集之中,选择一个元素作为"基准"(pivot). (2)所有小于"基准"的元 ...

  4. QuickSort 快速排序 基于伪代码实现

    本文原创,转载请注明地址 http://www.cnblogs.com/baokang/p/4737492.html 伪代码 quicksort(A, lo, hi) if lo < hi p ...

  5. quicksort

    快排.... void quicksort(int *a,int left,int right){ if(left >= right){ return ; } int i = left; int ...

  6. 随手编程---快速排序(QuickSort)-Java实现

    背景 快速排序,是在上世纪60年代,由美国人东尼·霍尔提出的一种排序方法.这种排序方式,在当时已经是非常快的一种排序了.因此在命名上,才将之称为"快速排序".这个算法是二十世纪的七 ...

  7. Teleport Ultra 下载网页修复

    1 三个基本正则替换 tppabs="h[^"]*"/\*tpa=h[^"]*/javascript:if\(confirm\('h[^"]*[Ult ...

  8. Ultra Video Splitter & Converter

    1. Video Splitter http://www.aone-soft.com/splitter.htm Ultra Video Splitter 是一款视频分割工具.可将一个巨大的AVI/Di ...

  9. 算法实例-C#-快速排序-QuickSort

    算法实例 ##排序算法Sort## ### 快速排序QuickSort ### bing搜索结果 http://www.bing.com/knows/search?q=%E5%BF%AB%E9%80% ...

  10. mac 激活Ultra Edit16

    一.文本编辑器UltraEdit 参照Ultra Edit16.10 Mac 破解下载,或者官方下载 Ultra Edit16即可 printf of=/Applications/UltraEdit. ...

随机推荐

  1. 无光驱安装原版 windows server2008,win7 的方法,64位的。

    这几天要对一台服务器进行安装 windows server2008的系统,64位.尼玛在网上买了一个光驱迟迟不到所以只能用U盘来了 以前安装ghost的系统U盘分分钟搞定.安装原版的iso文件遇到了一 ...

  2. java 中Handler 和Runnable 的使用 异步发送消息 转

    public class MainActivity extends Activity { TextView text1, text2; Button button; Thread th; @Overr ...

  3. valueOf和toString

    有一道经典的题目: var add = function() {___}; console.log(add(3)(4)(5)); // 输出60 题目要求能无限相乘,请补充add函数. 首先很显然,a ...

  4. AngularJS - 路由入门

    我们有很多方法让一个视图随着用户的操作进行变化. 但是,只是单单一个视图就要满足所有的需求会让代码变得非常复杂. 也许我们可以使用ng-include来引用各种模板,但这只限于部分场景. 于是我们可以 ...

  5. α发布后的感想(组长作业)

    今天α发布后,组长作业,谈谈心得体会,谈谈哪些做的好的地方,哪些做的不好.耐撕团队组长因有事缺席,耐撕团队的α发布由齐同学来主持,所以这个作业由齐同学代理. 先谈谈耐撕团队在α发布会中齐同学认为做的好 ...

  6. 超市管理系统—NABCD模型

    1) N (Need 需求) 需求分析: 超市的数据和业务越来越庞大,而计算机就是一种高效的管理系统,这就需要我们把超市的管理与计算机结合起来,从而超市管理系统应运而生.依靠现代化的计算机信息处理技术 ...

  7. 获取request的变量

    由于IP代码未实现,先注释掉. package com.helloweenvsfei.servlet; import java.io.IOException; import java.io.Print ...

  8. Sping MVC-创建HelloWeb项目(一)

    下面的例子显示怎样使用Spring MVC框架写一个简单的基于Web的应用程序,使用Eclipse IDE作为开发工具,按照下面的步骤使用Spring Web框架来开发一个动态的Web应用 步骤 描述 ...

  9. Linux Kernel中获取当前目录方法(undone)

    目录 . 引言 . 基于进程内存镜像信息struct mm_struct获取struct path调用d_path()获取当前进程的"绝对路径" . 基于文件描述符(fd).tas ...

  10. Linux登录验证机制、SSH Bruteforce Login学习

    相关学习资料 http://files.cnblogs.com/LittleHann/linux%E4%B8%AD%E7%94%A8%E6%88%B7%E7%99%BB%E5%BD%95%E8%AE% ...