题目网址: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. C#如何更好地理解引用类型和值类型

    说道值类型和引用类型,在C#中,官方的说法就是: 值类型直接指向数据:一般包括C#自带的所有数字类型,字符类型,bool类型,当然还有自定义的结构类型和枚举类型 而引用类型则是指向数据存储的地址.一般 ...

  2. PD16 Generate Datebase For Sql2008R2时报脚本错误“对象名sysproperties无效”

    PowerDesinger16创建数据库表到SQL2008R2时,执行报“对象名sysproperties无效”错误. 主要是在建模时我们对表.列增加了些说明注释,而Sql2005之后系统表syspr ...

  3. Qt编写自定义控件大全

    最新版可执行文件 http://pan.baidu.com/s/1i491FQP 不定期增加控件及修正BUG和改进算法. 总图: 1:动画按钮 * 1:可设置显示的图像和底部的文字 * 2:可设置普通 ...

  4. C#参考:Linq 概述

    Linq (Language Integrated Query,语言集成查询),是微软公司提供的一项新技术,它能够将查询功能引入到.NET 3.5 所支持的编程语言中,例如C#,Visual Basi ...

  5. SQL SERVER 使用select和union插入多条数据

    insert into A(A) select '2' union select '3' union select '100' go select * from A

  6. 1 Servlet开篇准备

    作者:禅楼望月(http://www.cnblogs.com/yaoyinglong) 1. HTTP协议 HTTP协议是TCP/IP协议的上层协议.TCP负责确保从一个网络节点向另一个网络节点发送的 ...

  7. 最近一段时间开发客户端app的感悟

    关于android和cocos2d 凭着对大学时候写html+css的一点点的记忆,我还是认为android的布局xml文件还是参考了html+css,只是他更加臃肿!就想 android平台本身那样 ...

  8. SQL实现分组查询取前几条记录

    我要实现的功能是统计订单日志表中每一个订单的前三条日志记录,表结构如下: 一个订单在定点杆日志表中有多条记录,要根据时间查询出每一个订单的前三条日志记录,sql如下: select b.OrderNu ...

  9. IOS开发UI基础UISwitch属性

    UISwitch属性1. onTintColor   处于on时switch 的颜色
    switchImage.onTintColor = [UIColor grayColor];2.tintC ...

  10. [Bootstrap]7天深入Bootstrap(2)整体架构

    大多数Bootstrap的使用者都认为Bootstrap只提供了CSS组件 和JavaScript插件,其实CSS组件和JavaScript插件只是Bootstrap框架的表现形式而已,它们都是构建在 ...