归并排序 (求逆序数)

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

时间复杂度: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. The basic introduction to MIX language and machine

    reference: The MIX Computer, The MIX Introduction sets, The basic info storage unit in MIX computer ...

  2. break的使用例一

    /* Name:break的使用例一 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月21日 02:28:24 Description:本程序代码无如何含 ...

  3. Open开发平台,认证,授权,计费

    1.申请appid和appkeyhttp://wiki.connect.qq.com/%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C_oauth2-0 appid:应用的唯一 ...

  4. css包含块containing block

    <css权威指南>P167: The Containing Block Every element is laid out with respect to its containing b ...

  5. 有关Gcd,Lcm的一点小结论

    先介绍两个: 大数的Gcd Stein+欧几里德 function stein(a,b:int64):int64; begin if a<b then exit(stein(b,a)); the ...

  6. SGU 201 Non Absorbing DFA (DP)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出一个自动机,给出所有的转移,其中还有一个 ...

  7. 编译ycm库

    在安装完YCM之后,重新打开vim还会出现如下的报错信息:ycm_client_support.[so|pyd|dll] and ycm_core.[so|pyd|dll] not detected; ...

  8. linux多线程示例

    #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h& ...

  9. css 3种清除浮动方法

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...

  10. Jquery $.extend的重载方法详述

    1 $.extend(result,item1,item2,item3,........)  -这个重载方法主要是用来合并,将所有的参数都合并到result中,并返回result,但是这样会破坏res ...