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 ...
随机推荐
- 百度云BDCloudVideoView播放器的初体验
今天试用了一下百度云BDCloudVideoView,记录下遇到的坑. 前面一切还好,按照他的要求各种导入,然后开始码代码,起实就是抄例子.然后各种坑开始了 下面这个你看名称能知道它是个啥吗? mVi ...
- javascript js自执行函数
javascript 自执行函数 一.自执行函数几种写法: 写法一: ( function(){ //代码 } )(); 写法二: ( function(){ //代码 }()); 二.作用: 隔离 ...
- Android Studio无法找到tool.jar解决方法!
今天安装并配置了JDK,可以在DOS窗口中使用“java -version”命令查看JAVA版本信息了,随后安装Android Studio,但是等Android Studio安装完毕,启动时候发现, ...
- App Store提交审核报错 ERROR ITMS-90087解决办法
1.原因说明 app对Wifi进行配网, 使用了GizWifiSDK.framework提交App Store时候报错了 App Store Connect Operation Error ERROR ...
- BST_insert
#include <stdio.h> /* printf, scanf, NULL */ #include <stdlib.h> /* malloc, free */ stru ...
- List_Delete
/*Sorting from little to large use List*/ #include <stdio.h> /* printf, scanf, NULL */ #includ ...
- 【ARC074F】Lotus Leaves 最小割
Description 给你一个n*m网格图,有起点荷叶和终点荷叶,有中转荷叶,其他的格子没东西,一个荷叶可以跳到同一行或者列的另一个荷叶.问最多删掉几个中转荷叶能让起点终点不连通.如果不行输出-1. ...
- node创建一个简单的web服务
本文将如何用node创建一个简单的web服务,过程也很简单呢~ 开始之前要先安装node.js 1.创建一个最简单的服务 // server.js const http = require('http ...
- Python数组(二)
一.函数list 可将任何序列(如字符串)作为list的参数.list实际上是一个类,而不是函数. test=['java','C#','C','C++'] print(list(test)) ——& ...
- Array数组结构底层实现复习
Array数组结构底层实现复习 内容待总结: size capacity length