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 ...
随机推荐
- c# 简单委托例子
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Redis 占用Windows系统盘空间23G
Redis常出现问题总结: 1.当出现修改--maxheap and --heapdir 在启动这两个版本时都会创建一个 RedisQFork.dat文件,我不确定 RedisQFork 文件是否变小 ...
- Canvas保存为图片
public static void GenerateCanvas(string imgSaveName, int canvasWidth, int canvasHeight, string imgD ...
- JS Img对象获取图片高度宽度(兼容Chrome)
一般获取图片高度宽度的写法: var img = new Image();img.src = imgsrc;var imgWH = CalcImgTiple(img.width, img.height ...
- CHOCBase
1.基础常用知识点 1.1 监听某个对象变化的四种方式 代理监听Delegate Notification通知 KVO键值监听 Block代码块 addTarget方法 1.2 音频视频相册等 2.常 ...
- Atcoder Grand Contest 031C(构造,思维,异或,DFS)
#include<bits/stdc++.h>using namespace std;int n,a,b,sum;void dfs(int x,int y,int ban){ if( ...
- mybatis 学习笔记(二):mybatis SQL注入问题
mybatis 学习笔记(二):mybatis SQL注入问题 SQL 注入攻击 首先了解下概念,什么叫SQL 注入: SQL注入攻击,简称SQL攻击或注入攻击,是发生于应用程序之数据库层的安全漏洞. ...
- hadoop的maven地址
https://mvnrepository.com/artifact/org.apache.hadoop
- SDUT OJ 数据结构实验之链表一:顺序建立链表
数据结构实验之链表一:顺序建立链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- 自定义内核启动后的Logo
1.使用图像GIMP工具 2.详细步骤如下: A.将800x480的图片导入到GIMP工具. B.选中GIMP菜单栏进行以下操作 图像 -->模式 ...