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
Output
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的更多相关文章
- quickSort算法导论版实现
本文主要实践一下算法导论上的快排算法,活动活动. 伪代码图来源于 http://www.cnblogs.com/dongkuo/p/4827281.html // imp the quicksort ...
- Javascript算法系列之快速排序(Quicksort)
原文出自: http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/ https://gis ...
- JavaScript 快速排序(Quicksort)
"快速排序"的思想很简单,整个排序过程只需要三步: (1)在数据集之中,选择一个元素作为"基准"(pivot). (2)所有小于"基准"的元 ...
- QuickSort 快速排序 基于伪代码实现
本文原创,转载请注明地址 http://www.cnblogs.com/baokang/p/4737492.html 伪代码 quicksort(A, lo, hi) if lo < hi p ...
- quicksort
快排.... void quicksort(int *a,int left,int right){ if(left >= right){ return ; } int i = left; int ...
- 随手编程---快速排序(QuickSort)-Java实现
背景 快速排序,是在上世纪60年代,由美国人东尼·霍尔提出的一种排序方法.这种排序方式,在当时已经是非常快的一种排序了.因此在命名上,才将之称为"快速排序".这个算法是二十世纪的七 ...
- Teleport Ultra 下载网页修复
1 三个基本正则替换 tppabs="h[^"]*"/\*tpa=h[^"]*/javascript:if\(confirm\('h[^"]*[Ult ...
- Ultra Video Splitter & Converter
1. Video Splitter http://www.aone-soft.com/splitter.htm Ultra Video Splitter 是一款视频分割工具.可将一个巨大的AVI/Di ...
- 算法实例-C#-快速排序-QuickSort
算法实例 ##排序算法Sort## ### 快速排序QuickSort ### bing搜索结果 http://www.bing.com/knows/search?q=%E5%BF%AB%E9%80% ...
- mac 激活Ultra Edit16
一.文本编辑器UltraEdit 参照Ultra Edit16.10 Mac 破解下载,或者官方下载 Ultra Edit16即可 printf of=/Applications/UltraEdit. ...
随机推荐
- Android 中R文件丢失问题解决方案
Project → clean 项目上右键→android Tools→ fix project 检查xml文件中有无命名错误,特别是@+id写成@id的[特别是这条,注意看控制台打印的xml错误]
- (转)Android Binder设计与实现 – 设计篇
原文地址(貌似已打不开):Android Binder设计与实现 – 设计篇 ------------------------------------------------------------- ...
- 【niubi-job——一个分布式的任务调度框架】----安装教程
niubi-job是什么 niubi-job是LZ耗时三个星期,费尽心血打造的一个具备高可靠性以及水平扩展能力的分布式任务调度框架,采用quartz作为底层的任务调度管理器,zookeeper做集群的 ...
- 编写高质量代码改善C#程序的157个建议[避免finaly内的无效代码、避免嵌套异常、避免吃掉异常、注意循环异常处理]
前言 本文已同步到http://www.cnblogs.com/aehyok/p/3624579.html.本文主要来学习以下几点建议 建议61.避免在finally内撰写无效代码 建议62.避免嵌套 ...
- [JQuery EasyUI系列]简介
一.jQuery EasyUI是一个基于jQuery的框架,继承了各种用户界面插件. 二.jQuery EasyUI框架提供了创建网页所需的一切,可以轻松建立站点. easyui是一个基于jQuery ...
- Teradata(不同date输出要求;表类型)
1. 需要某种特定形式的date 类型export 到文件中,例如 YYYYMMDD/ YYYY-MM-DD 这时候不一定非要用date 类型,可以转换为varchar 类型! CAST(CAST ( ...
- Excel 计算 tips
1. 对一列数据想看看,distinct的结果 选中数据区域(包含列名),插入pivot table 2. 想检查一个单元格的值在不在某一列中,并返回标志值 =IF (COUNTIF(B:B,A1) ...
- Java编程思想学习(十二) 数组和容器
一.数组 1).数组的多种初始化方式 下面总结了初始化数组的多种方式,以及如何对指向数组的引用赋值,使其指向另一个数组对象.值得注意的是:对象数组和普通数组的各种操作基本上都是一样的:要说有什么不同的 ...
- Bzoj3943 [Usaco2015 Feb]SuperBull
3943: [Usaco2015 Feb]SuperBull Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 300 Solved: 185 Desc ...
- vim YouCompleteMe
http://www.ithao123.cn/content-1906969.html http://www.it165.net/os/html/201503/12190.html