描述

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

题目来源

Waterloo Feb.5 2005

求最小的交换次数,即求逆序对数问题,并归排序。

【并归排序】

如下演示并归排序的整个过程:

并归排序主要是深搜实现的。

{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的更多相关文章

  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. TOJ 2776 CD Making

    TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性...  贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...

  8. Teleport Ultra 下载网页修复

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

  9. Ultra Video Splitter & Converter

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

随机推荐

  1. 用JS实现省市二级联动

    一.需求分析 我们希望在注册页面中添加一个字段(籍贯),当用户选择一个具体的省份,在后面的下拉列表中动态加载该省份下所有的城市.显示的效果如下: 二.技术分析 使用事件(onchange) 使用一个二 ...

  2. HackTwelve 为背景添加圆角边框

    1.概要:     ShapeDrawable是一个为UI控件添加特效的好工具.这个技巧适用于那些可以添加背景的控件 2.添加圆角边框其实就是添加的背景那里不是直接添加图片,而是添加一个XML文件即可 ...

  3. [raspberry p3] suse wifi驱动加载

    问题 raspberry pi3安装后发现wifi 启动不了, brcmf_sdio加载失败了,return error code为-110 处理方法 打开 /etc/dracut.conf.d/ra ...

  4. storm集群快速搭建

    sudo mkdir /export/serverssudo chmod -R 777 /exportmkdir /export/servers tar -zxvf apache-storm-1.0. ...

  5. javaee--学生成绩录入与显示--Struts2标签的使用

    类Score.java:各课程的成绩及平均成绩 类Student.java:学生姓名.学号及Score类 类ScoreAction.java:将Student类存在一个List对象中, execute ...

  6. (获取qq群成员信息,并下载头像,每个群保存一个文件夹)

    # 1.获取到自己qq里面所有的群,并且保存每个群里面的群成员信息到mongodb里面# 下载每个群的群成员的头像# 1.抓包,抓到获取自己所有qq群的接口 requests模块 https://qu ...

  7. 洛谷P4011 孤岛营救问题(状压+BFS)

    传送门 和网络流有半毛钱关系么…… 可以发现$n,m,p$都特别小,那么考虑状压,每一个状态表示位置以及钥匙的拥有情况,然后每次因为只能走一步,所以可以用bfs求出最优解 然后是某大佬说的注意点:每个 ...

  8. flink学习笔记-数据源(DataSource)

    说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...

  9. docker 安装 postgresql

    docker拉去镜像以及配置生成容器的步骤几乎和之前的nginx安装一样,直接写下面的命令了 1. docker pull postgres 2. docker run --name xf-postg ...

  10. 斐讯 N1 刷 Armbian 5.64

    前言 N1 天天链是斐讯出的一款挖矿产品,虽然已经翻车,但是本身硬件配置还是很不错的,晶晨 S905D 主控,蓝牙 4.1,双频 WiFi,2G + 8G,USB2.0,HDMI.而一个只要不到 80 ...