TOJ 2452 Ultra-QuickSort
描述
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.
输入
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.
输出
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.
样例输入
5
9
1
0
5
4
3
1
2
3
0
样例输出
6
0
题目来源
求最小的交换次数,即求逆序对数问题,并归排序。
【并归排序】
如下演示并归排序的整个过程:
并归排序主要是深搜实现的。
{9,1,0,4,5,8,7,4,3}=>{9,1,0,4,5} {8,7,4,3}
{9,1,0,4,5}=>{9,1,0} {4,5}=>{9}{1}{0} {4}{5}
{8,7,4,3}=>{8,7} {4,3}=>{8}{7} {4}{3}
合并子表
{0,1,9} {4,5} {7,8} {3,4}
{0,1,4,5,9} {3,4,7,8}
{0,1,3,4,4,7,8,9}
#include <stdio.h>
__int64 sum;
void mergeSort(int* a,int low,int mid,int high){
int* p=new int[high+1];
int i=low;
int j=low;//左侧表的起始位置
int h=mid+1;//右侧表的起始位置
while(h<=high&&j<=mid){
if(a[j]<=a[h]){
p[i]=a[j];
j++;
i++;
}else{
//求逆序数
sum+=h-i;
p[i]=a[h];
h++;
i++;
}
}
for(;j<=mid;j++,i++){
p[i]=a[j];
}
for(;h<=high;h++,i++){
p[i]=a[h];
}
for(i=low;i<=high;i++){
a[i]=p[i];
}
delete[] p;
} void merge(int* a,int low,int high){
if(low<high){
int mid=(low+high)>>1;
//划分子表
merge(a,low,mid);
merge(a,mid+1,high);
//合并子表
mergeSort(a,low,mid,high);
}
} int main()
{
int n;
int arr[500010];
while(scanf("%d",&n)!=EOF && n){
for(int i=0; i<n; i++){
scanf("%d",&arr[i]);
}
sum=0;
merge(arr,0,n-1);
printf("%I64d\n",sum);
}
return 0;
}
TOJ 2452 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年代,由美国人东尼·霍尔提出的一种排序方法.这种排序方式,在当时已经是非常快的一种排序了.因此在命名上,才将之称为"快速排序".这个算法是二十世纪的七 ...
- TOJ 2776 CD Making
TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性... 贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...
- 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 ...
随机推荐
- delphi取括号内或括号外的内容
function TSetParkForm.RemoveSgin(str: string): string; // 去掉括号内的内容(包括括号) var i1, i2, i: integer; beg ...
- DataTable中的select()用法
1.在DataTable中执行DataTable.Select("条件")返回DataTable // <summary> // 执行DataTable中的查询返回新的 ...
- (一)springmvc+spring+mybatis+maven框架搭建
(一)springmvc+spring+mybatis+maven框架搭建 1.说明 工作之余,为了学习点东西.先搭建个框架. 以后要往里面加东西,比如rabbitMQ.redis.shiro等. 也 ...
- 多态实现的原理------新标准c++程序设计
“多态”的关键在于通过基类指针或引用调用一个虚函数时,编译时不确定到底调用的是基类还是派生类的函数,运行时才确定.例子: #include<iostream> using namespac ...
- this指针------新标准c++程序设计
背景: c++是在c语言的基础上发展而来的,第一个c++的编译器实际上是将c++程序翻译成c语言程序,然后再用c语言编译器进行编译.c语言没有类的概念,只有结构,函数都是全局函数,没有成员函数.翻 ...
- Kotlin 函数
至于什么函数,在计算机里面就是一个密闭的执行程序的代码块(个人理解) 我们先来看看什么是函数 fun main(agrs : Array<String>) { println(" ...
- 《javascript 高级程序设计》 笔记2 8~章
chapter 8 BOM(浏览器对象模型) window对象 表示浏览器的一个实例. 直接在window对象上定义的属性可以通过delete操作符删除,而全局变量不可以. 窗口关系及框架 位置操作 ...
- 一大波趣图:CSS的力量
CSS的力量 CSS的作用,一目了然~ 见识一下CSS的厉害! 用了CSS,效果显著 HTML5 + CSS3 + Javascript会怎么样? HTML ...
- Codeforces Round #551 (Div. 2)A. Serval and Bus
A. Serval and Bus time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- clickonce发布方式创建桌面快捷方式
1.工程属性->发布->选项->清单:创建桌面快捷方式打勾 2.工程属性->应用程序->清单:下拉列表选择Properties\app.manifest(其中的图标可以选 ...