描述

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. Glide4.0使用

    导入 dependencies { compile 'com.github.bumptech.glide:glide:4.0.0' compile 'com.android.support:suppo ...

  2. C# System.Threading.Timer 定时器

    前提: 需要引入  System.Threading: 描述: 在很多时间我们都需要进行延迟执行,或是定时执行一些指定业务,这个时候使用 Timer 是最合适的,而且 Timer 是Cpu 级别处理对 ...

  3. angular 守卫路由

    import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; im ...

  4. 6步完成压力测试工具Locust部署和使用

    1,准备安装python,安装过程略 已安装的,查看安装目录: cmd输入where Python 2,pip安装locust 1.进入python所在目录,如果没有配置环境变量,需要进入到C:\Us ...

  5. C# LINQ(8)

    回顾之前的代码都是LINQ自行推断类型.其实LINQ在查询的结束是可以动态创建类型. , , , , , , , , , , }; var list = from num in intArray &a ...

  6. uoj#344. 【清华集训2017】我的生命已如风中残烛(计算几何)

    题面 传送门 题解 orzxyx 首先我们发现,一个点如果被到达大于一次,那么这个点肯定在一个环上.所以在不考虑环的情况下每个点只会被到达一次,那么我们就可以直接暴力了 简单来说,我们对每个点\(i\ ...

  7. django自定义rbac权限组件(二级菜单)

    一.目录结构 二.表结构设计 model.py from django.db import models # Create your models here. class Menu(models.Mo ...

  8. #首行输入数n,接下来输入n行数,以空格隔开

    #首行输入数n,接下来输入n行数,以空格隔开 n = int(raw_input())# print nL = []for i in range(n): L.append([int(x) for x ...

  9. 第3项:用私有构造器或者枚举类型强化Singleton属性

      Singleton指仅仅被实例化一次的类 [Gamma95].Singleton通常代表无状态的对象,例如函数(第24项)或者本质上唯一的系统组件.使类称为Singleton会使它的客户端测试变得 ...

  10. audio.play dom对象 JQ不支持play

    */        PausePlayVoice:function() {            $("#spPauseAudio").click(function() {     ...