#include <iostream>
const int MAX = ;
int a[MAX];
int swap[MAX]; //临时数组
int n; //数组a的长度
__int64 result; //数组a中的逆序数 //归并两个已经有序的段:a[low]—a[mid]和a[mid+1]—a[high],使得a[low]—a[high]有序。
void merge(int low, int mid, int high)
{
int i = low;
int j = mid + ;
int m = ;
while(i <= mid && j <= high)
{
if(a[i] <= a[j])
{
swap[m++] = a[i];
i++;
}
else
{
swap[m++] = a[j];
j++;
result += mid - i + ;
}
}
while(i <= mid)
{
swap[m++] = a[i];
i++;
}
while(j <= mid)
{
swap[m++] = a[j];
j++;
}
for(i = ; i < m; i++)
a[low + i] = swap[i];
} //归并排序:对a[low]—a[high]进行归并排序。
void mergeSort(int low, int high)
{
int mid;
if(low < high)
{
mid = (low + high) /;
mergeSort(low, mid);
mergeSort(mid + , high);
merge(low, mid, high);
}
} int main()
{
int i;
while(true)
{
scanf("%d",&n);
if(n == ) break;
result = ;
for(i = ; i < n; i++)
scanf("%d",a+i);
mergeSort(, n-);
printf("%I64d\n",result);
}
return ;
}

poj 2299 求逆序数的更多相关文章

  1. POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树

    题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...

  2. POJ 2299 求逆序对个数 归并排序 Or数据结构

    题意: 求逆序对个数 没有重复数字 线段树实现: 离散化. 单点修改,区间求和 // by SiriusRen #include <cstdio> #include <cstring ...

  3. poj 2299 Ultra-QuickSort (归并排序 求逆序数)

    题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...

  4. POJ 2299 Ultra-QuickSort 归并排序、二叉排序树,求逆序数

    题目链接: http://poj.org/problem?id=2299 题意就是求冒泡排序的交换次数,显然直接冒泡会超时,所以需要高效的方法求逆序数. 利用归并排序求解,内存和耗时都比较少, 但是有 ...

  5. poj 2299 Ultra-QuickSort(树状数组求逆序数)

    链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...

  6. poj 2299 Ultra-QuickSort 归并排序求逆序数对

    题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...

  7. poj 2299 Ultra-QuickSort :归并排序求逆序数

    点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34676   Accepted ...

  8. POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 70674   Accepted: 26538 ...

  9. 题解报告:poj 2299 Ultra-QuickSort(BIT求逆序数)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

随机推荐

  1. IIS URL重写找不到页面 (URLRewriter.dll伪静态)

    在网站上点右键 属性 进入主目录菜单 点击配置 找到.html扩展名 编辑 将 检查文件是否存在 的钩去掉! OK

  2. 逻辑回归的分布式实现 [Logistic Regression / Machine Learning / Spark ]

    1- 问题提出 2- 逻辑回归 3- 理论推导 4- Python/Spark实现 # -*- coding: utf-8 -*- from pyspark import SparkContext f ...

  3. Custom PeopleSoft Queries

      The following SQL identifies custom queries created in your system from the PSQRYDEFN PeopleTools ...

  4. Check Box Select/Deselect All on Grid

    The below function is to be used on a grid with multiple check boxes. Place the code behind a FieldC ...

  5. 实现Win7远程桌面关机和重启

    通过远程桌面控制Win7系统时,菜单中没有关机和重启按钮, 1.方法1 关机 shutdown -s -t 0重启 shutdown -r -t 0 可以先打开运行框(Win+R键),输入上述命令即可 ...

  6. 有关c#装箱和拆箱知识整理

    c#装箱和拆箱知识,装箱和拆箱是一个抽象的概念. 1.装箱和拆箱是一个抽象的概念  2.装箱是将值类型转换为引用类型 : 拆箱是将引用类型转换为值类型 利用装箱和拆箱功能,可通过允许值类型的任何值与O ...

  7. centos6.7下网络设置

    vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0"BOOTPROTO="static"   # ...

  8. js 月历 时间函数 月份第一天 星期的判断

    返回值为0-6,其中返回值0为星期天:如同,php中的日期函数一样判断.

  9. 苹果Mac OS系统shell命令大全介绍

    基本命令 1.列出文件 ls 参数 目录名        例: 看看驱动目录下有什么:ls /System/Library/Extensions 参数 -w 显示中文,-l 详细信息, -a 包括隐藏 ...

  10. .NET设计模式系列文章 from TerryLee

    http://www.cnblogs.com/Terrylee/archive/2006/07/17/334911.html 最初写探索设计模式系列的时候,我只是想把它作为自己学习设计模式的读书笔记来 ...