博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TOJ 2452 Ultra-QuickSort
阅读量:4599 次
发布时间:2019-06-09

本文共 1975 字,大约阅读时间需要 6 分钟。

描述

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.

样例输入

59105431230

样例输出

60

题目来源

 

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

 

【并归排序】

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

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

{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 
__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
>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

 

 

转载于:https://www.cnblogs.com/chenjianxiang/p/3538721.html

你可能感兴趣的文章
SQLAlchemy
查看>>
BZOJ 1303: [CQOI2009]中位数图 问题转化_扫描_思维
查看>>
SP1026 FAVDICE - Favorite Dice 数学期望
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
今日内容的回顾12
查看>>
js中字符串常用熟悉和方法
查看>>
【矩阵+十进制快速幂】[NOI2013]矩阵游戏
查看>>
Java一个简单的文件工具集
查看>>
蓝牙BLE扫描成功,log中打印出扫描到的设备
查看>>
React(v16.8.4)生命周期详解
查看>>
一般处理应用页中绑定方法代码段
查看>>
React组件Components的两种表示方式
查看>>
无限鼠标没反应了
查看>>
CSU - 1356 Catch(dfs染色两种写法,和hdu4751比较)
查看>>
zabbix监控php-fpm的性能
查看>>
温故知新 div + css笔记
查看>>
针对降质模型中的模糊SR
查看>>
ios开发学习笔记001-C语言基础知识
查看>>
POJ1142Smith Numbers一道简单的数学题
查看>>
UIButton(改变Title和image位置)
查看>>