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. 用SugarORM快速开发需要同步和保存大量数据的Android互联网客户端

    最近开发的一个项目主要有两个特点,这两点也是在项目开发前需要着重去规划解决方案的: 需要和Rest服务端请求大量的数据 同时这些数据本地也要保存到sqlite数据库 对于第一点,目前的Volley.G ...

  2. PHP PC端接入支付宝和微信感悟

    想着中秋节的时候把异步线程学习完,同事说有个PHP的支付要帮忙做一下,虽然中秋节计划是把C#的异步学完,不过还是喜欢挑战,好久没有像大学一样这么认真的熬夜解决问题了.由于在大学学过asp,它和php有 ...

  3. onload是代码在也买你的追加元素的完成,而不是http请求的完成

  4. 模态窗口插件之Jbox

    $.jBox.tip("报损数量不能大于库存数!", 'error'); $.jBox.tip("请选择要报损的产品", "warn"); ...

  5. [bzoj 1026]windy数(数位DP)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1026 分析: 简单的数位DP啦 f[i][j]表示数字有i位,最高位的数值为j的windy数总 ...

  6. Bootstrap3.0学习第十五轮(大屏幕介绍、页面标题、缩略图、警示框、Well)

    详情请查看 http://aehyok.com/Blog/Detail/22.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:h ...

  7. Symfony学习--目录和入口

    1 目录结构 根目录下有: app src vendor web app是存放应用的一些配置文件,如果有一些配置文件或者文档,应当存放在这里面. src是存放你的项目的php代码,这里的php至少必须 ...

  8. go语言的模板,text/template包

    go语言的模板,text/template包 定义 模板就是将一组文本嵌入另一组文本里 传入string--最简单的替换 package main import ( "os" &q ...

  9. 列表 list

    例子: #!/usr/bin/python li = list([11,22,33,66,99,77]) print(li) li.extend((44,55,)) print(li) ret = l ...

  10. BZOJ-2929 洞穴攀岩 最大流Dinic(傻逼题)

    竟然没有1A真羞耻...1分钟不到读完题,10分钟不到打完....MD没仔细看...WA了一遍,贱! 2929: [Poi1999]洞穴攀行 Time Limit: 1 Sec Memory Limi ...