归并排序 (求逆序数)

归并排序:递归+合并+排序

时间复杂度:O(n logn)    空间复杂度:O(n)

用途:1.排序  2.求逆序对数

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 sequence9 1 0 5 4 ,        Ultra-QuickSort produces the output0 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的无序序列,每次只能交换相邻的两个数,求至少要交换多少次才使得该序列为递增序列。 分析:
1.根据时间范围估计时间复杂度,归并排序的时间复杂度一般为O(n logn)
2.算法步骤: 1.划分问题:把序列分为元素个数尽量相等的两半
2.递归求解:把两半元素分别排序
3.合并问题:把两个有序表合成一个
3.保存时,用long long 类型,否则会wa的 代码:
 #include<cstdio>
#include<iostream>
using namespace std;
const int maxn=; int a[maxn],b[maxn];
long long ans; void merge(int l,int m,int r) //归并排序
{
int i=l,j=m+,t=;
while(i<=m&&j<=r) //从小到大排序
{
if(a[i]>a[j])
{
b[t++]=a[j++];
ans+=m-i+;
}
else b[t++]=a[i++];
}
while(i<=m)
b[t++]=a[i++];
while(j<=r)
b[t++]=a[j++];
for(int i=;i<t;i++) //合并
a[l+i]=b[i];
} void merge_sort(int l,int r) //划分问题
{
if(l<r)
{
int m=(l+r)/;
merge_sort(l,m);
merge_sort(m+,r);
merge(l,m,r);
}
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==)
break;
ans=;
for(int i=;i<n;i++)
scanf("%d",&a[i]);
merge_sort(,n-);
printf("%lld\n",ans);
}
return ;
}

本来想着完全不看别人的代码都由自己做,可是看了一天了只是有了思路,还是写不出来。。。又看来了别人的代码。。。

A.归并排序的更多相关文章

  1. 算法与数据结构(十五) 归并排序(Swift 3.0版)

    上篇博客我们主要聊了堆排序的相关内容,本篇博客,我们就来聊一下归并排序的相关内容.归并排序主要用了分治法的思想,在归并排序中,将我们需要排序的数组进行拆分,将其拆分的足够小.当拆分的数组中只有一个元素 ...

  2. [算法]——归并排序(Merge Sort)

    归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...

  3. 归并排序的java实现

    归并排序的优点不说了. 做归并排序之前,我先试着将两个有序数组进行排序,合并成一个有序数组. 思路:定义好两个有序数组,理解的时候我先思考了数组只有一个数组的排序,然后是两个元素的数组的排序,思路就有 ...

  4. JavaScript算法(归并排序与快速排序)

    归并排序与快速排序这两个算法放在一起,也是因为时间复杂度都是对数级别的. 目前看过的资料,归并排序看<学习JavaScript数据结构与算法>介绍的归并排序吧,快速排序直接看百度百科,讲的 ...

  5. 归并排序算法 java 实现

    归并排序算法 java 实现 可视化对比十多种排序算法(C#版) [直观学习排序算法] 视觉直观感受若干常用排序算法 算法概念 归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Di ...

  6. java归并排序,单线程vs多线程

    一.什么是归并排序 归并排序又称合并排序,它是成功应用分治技术的一个完美例子.对于一个需要排序的数组A[0..n-1],归并排序把它一分为二:A[0..n/2-1]和A[n/2..n-1],并对每个子 ...

  7. sphinx索引分析——文件格式和字典是double array trie 检索树,索引存储 – 多路归并排序,文档id压缩 – Variable Byte Coding

    1 概述 这是基于开源的sphinx全文检索引擎的架构代码分析,本篇主要描述index索引服务的分析.当前分析的版本 sphinx-2.0.4 2 index 功能 3 文件表 4 索引文件结构 4. ...

  8. php基础排序算法 冒泡排序 选择排序 插入排序 归并排序 快速排序

    <?php$arr=array(12,25,56,1,75,13,58,99,22);//冒泡排序function sortnum($arr){    $num=count($arr);    ...

  9. [NOIP2013] 火柴排队(归并排序)

    题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为: ∑(ai-bi)^2 其中 ai 表示 ...

  10. 用Java写算法之归并排序

    转自:http://flyingcat2013.blog.51cto.com/7061638/1281026 前面的三种排序算法(冒泡排序,选择排序,插入排序)在平均情况下均为O(n^2)复杂度,在处 ...

随机推荐

  1. AndroidStudio 使用Hide API

    1.反射法 速度慢 2.生成新的android.jar 通常需要隐藏API的地方并不多 不需要整个都编译 而且编译出的framework.jar也不全 缺少java.*和javax.* 所以只把需要的 ...

  2. 在实体对象中访问导航属性里的属性值出现异常“There is already an open DataReader associated with this Command which must be closed first”

    在实体对象中访问导航属性里的属性值出现异常“There is already an open DataReader associated with this Command which must be ...

  3. discuz门户文章页面模板修改

    修改内容:view.htm 1.文章标题,模板代码 <h1 class="ph">$article[title] <!--{if $article['status ...

  4. linux环境变量配置总结

    LD_LIBRARY_PATH: 动态库的查找路径设置:方法一: export  LD_LIBRARY_PATH=LD_LIBRARY_PATH:/XXX 但是登出后就失效方法二: 修改~/.bash ...

  5. 如何注册Uber司机,加入uber(全国版最新最详细注册流程)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. 利用宏定义令iOS项目当中的NSLog不执行

    今天在博客园主页看到一篇帖子,提到NSLog消耗运行时性能: http://www.cnblogs.com/sunnyxx/p/3680623.html 解决方案如下,在​Prefix.pch文件当中 ...

  7. Tempter of the Bone(dfs奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. HDU1250:Hat's Fibonacci

    Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...

  9. unity 播放外部视频

    摘要: Unity支持的播放视频格式有.mov..mpg..mpeg..mp4..avi和.asf.只需将对应的视频文件拖拽入Project视图即可,它会自动生成对应的MovieTexture对象. ...

  10. HTML5入门(一)

    HTML简单介绍: HTML(HyperText Markup Language),超文本标记语言,是一种专门用于创建web的超文本文档编程语言,是我们看到的网页的源代码. 版本简介: 1997年推出 ...