题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109331#problem/A

Description

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.

Input

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.

Output

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.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0 题意:输入n个数,相邻两个数可以交换位置进行从小到大排序,求最小交换次数。 解题思路:用线段树的思想将数组一半一半的划分成小的区间,最后划分为只有两个数的区间,这两个数进行比较交换位置,记录交换次数就。先进行前两个数的比较,然后扩大区间为(left,right),
可知(left,right)区间中,(left,center)和(center+1,right)区间里的数已经排好序,将(center+1,right)中的数一个一个与(left,center)中的数比较大小,用另一个数组记录
新的排序后的位置(相当于向前插入数),并记录交换次数。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
int a[];
long long num;
int temp[]; void Merge(int arr[], int left, int center, int right)
{
int i=left;
int j=center+;
int k=;
while (i<=center&&j<=right)
{
///可知(left,right)区间中,(left,center)和(center+1,right)区间里的数已经排好序
if (arr[i] > arr[j])
{
temp[k++] = arr[j++];///
num+= center+-i;
}
else
temp[k++] = arr[i++];///记录小的数,以便得到排序后新的序列;
}
while (i <= center) ///
temp[k++] = arr[i++];
while (j <= right) ///这个和上个while语句两个while语句记录了原数组交换次序后的新的数组;
temp[k++] = arr[j++];
for (i = left, k = ; i <= right; i++, k++)///将array[]数组变为新的数组;
arr[i] = temp[k];
} void mergeSort(int arr[], int left, int right)
{
if (left<right)
{
int center = (left + right) / ;
mergeSort(arr, left, center); ///
mergeSort(arr, center + , right);///将数组划分为两个区间;
Merge(arr, left, center, right); ///将这个区间里的数进行排序;
}
} int main()
{
int n;
while(scanf("%d",&n)&&n)
{
num=;
for(int i=;i<n;i++)
scanf("%d",&a[i]);
mergeSort(a,,n-);
printf("%lld\n",num);
}
}

线段树——Ultra-QuickSort的更多相关文章

  1. bzoj3932--可持久化线段树

    题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...

  2. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

  3. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

  4. codevs 1080 线段树点修改

    先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...

  5. codevs 1082 线段树区间求和

    codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...

  6. PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树

    #44. [HNSDFZ2016 #6]可持久化线段树 统计 描述 提交 自定义测试 题目描述 现有一序列 AA.您需要写一棵可持久化线段树,以实现如下操作: A v p x:对于版本v的序列,给 A ...

  7. CF719E(线段树+矩阵快速幂)

    题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...

  8. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

  9. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

  10. 【BZOJ-2653】middle 可持久化线段树 + 二分

    2653: middle Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1298  Solved: 734[Submit][Status][Discu ...

随机推荐

  1. struts2:遍历自定义字符串数组,遍历Action实例所引用对象中的数组

    在struts2:OGNL表达式,遍历List.Map集合:投影的使用一文中已经讲述了OGNL遍历List.Map集合等功能. 本文简单写一个遍历数组的示范程序. 1. 遍历自定义字符串数组 < ...

  2. FFRPC应用之Client/Server

    摘要: Ffrpc 进行了重构,精简了代码,代码更加清晰简洁,几乎完美的达到了我的预想.接下来将写几遍文章来介绍ffrpc可以做什么.简单总结ffrpc的特性是: Ffrpc是c++ 网络通信库 全异 ...

  3. 编写并发程序 Inversion

    做完了 scala parallel 课程作业后,觉得 scala 写并发程序的便捷性是 java 永远都追不上的.scala 的Future 和 Promise,java 里 Future 和 Co ...

  4. HTML5新特性之WebRTC

    1.概述 WebRTC是“网络实时通信”(Web Real Time Communication)的缩写,它主要用来让浏览器实时获取和交换视频.音频和数据. WebRTC共分三个API. MediaS ...

  5. 关于Task类处理多线程简单示例

    1.定义一个线程 var task1 = Task.Factory.StartNew(() => DoSomeWork()):方法如下:          private static obje ...

  6. Ranorex入门指南

    Ranorex入门指南 http://automationqa.com/forum.php?mod=viewthread&tid=2766&fromuid=29

  7. pdf嵌入字体

    论文提交时,要求所有的字体都是嵌入的,为这个问题折腾了很久,发现了一个很好的答案,记一下: http://stackoverflow.com/questions/4231656/how-do-i-em ...

  8. SecureCRT使用

    SecureCRT可以说是linux远程终端的代名词,关于它的一些技巧必须掌握,,, 1.解决中文乱码 登陆主机,运行locale命令,确定语言选项LANG是否为 zh_CN.gb2312 或者 en ...

  9. 将main方法打成jar包,并引用第三方的maven jar包

    一.准备工作.执行命令 学习插件: 学习apache的打包插件maven-assembly-plugin:http://maven.apache.org/plugins/maven-assembly- ...

  10. 获取当前 Python 版本

    方法一 >>> from platform import python_version >>> print python_version() 2.7.8 方法二 & ...