#include<iostream>
#include<malloc.h>
using namespace std;
long long ans;
void merge(int *a,int le,int mid,int rt){
int *sort_data=(int *)malloc(sizeof(int)*(rt-le+));
if(!sort_data) return;
int i=le,j=mid+,pt=;
while(i<=mid&&j<=rt){
if(a[i]<=a[j]){
sort_data[pt++]=a[i++];
}
else{//exist swap action
sort_data[pt++]=a[j++];
/*
once we swap the postion of both the a[i] and a[j],
we do change the positon of a[i+1......mid]
so as a result, we do change the postion of a[i,i+1,....mid].
*/
ans+=mid-i+;
}
}
while(i<=mid){
sort_data[pt++]=a[i++];
}
while(j<=rt){
sort_data[pt++]=a[j++];
}
int p;
for(int i=le,p=;i<=rt;i++,p++){
a[i]=sort_data[p];
}
}
void mergeSort(int a[],int st,int ed){
if(st<ed){
int mid=(st+ed)/;
mergeSort(a,st,mid);
mergeSort(a,mid+,ed);
merge(a,st,mid,ed);
}
}
int main(){
int n,num[];
while(scanf("%d",&n),n){
ans=;
for(int i=;i<n;i++){
scanf("%d",&num[i]);
}
mergeSort(num,,n-);
printf("%I64d\n",ans);
}
return ;
}

POJ2299: Ultra-QuickSort-合并排序解决逆序数问题的更多相关文章

  1. poj2299解题报告(归并排序求逆序数)

    POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...

  2. poj2299--归并排序求逆序数

    /** \brief poj2299  *  * \param date 2014/8/5  * \param state AC  * \return memory 4640K time 3250ms ...

  3. [CF 351B]Jeff and Furik[归并排序求逆序数]

    题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...

  4. 线段树求逆序数方法 HDU1394&amp;&amp;POJ2299

    为什么线段树能够求逆序数? 给一个简单的序列 9 5 3 他的逆序数是3 首先要求一个逆序数有两种方式:能够从头開始往后找比当前元素小的值,也能够从后往前找比当前元素大的值,有几个逆序数就是几. 线段 ...

  5. nyoj 117 求逆序数 (归并(merge)排序)

    求逆序数 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中 ...

  6. poj 1007:DNA Sorting(水题,字符串逆序数排序)

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 80832   Accepted: 32533 Des ...

  7. POJ2299 Ultra-QuickSort(归并排序求逆序数)

    归并排序求逆序数   Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  8. hdu 1394 zoj 1484 求旋转序列的逆序数(并归排序)

    题意:给出一序列,你可以循环移动它(就是把后面的一段移动到前面),问可以移动的并产生的最小逆序数. 求逆序可以用并归排序,复杂度为O(nlogn),但是如果每移动一次就求一次的话肯定会超时,网上题解都 ...

  9. SDUT 3402 数据结构实验之排序五:归并求逆序数

    数据结构实验之排序五:归并求逆序数 Time Limit: 40MS Memory Limit: 65536KB Submit Statistic Problem Description 对于数列a1 ...

随机推荐

  1. 6.MVC框架开发(文件上传)

    1.需要设置表单的enctype="multipart/form-data"属性 2.在控制器中获取表单文件中数据 [HttpPost] public ActionResult A ...

  2. <一> MVC - HtmlHelper

    HtmlHelper类位于System.Web.Mvc.Html之中主要有七个静态类组成: FormExtensions - BeginForm, BeginRouteForm, EndForm In ...

  3. 当页面编辑或运行提交时,出现“从客户端中检测到有潜在危险的request.form值”问题,该怎么办呢?

    最近在学习highcharts时,关于其中的导出功能,本来是想把导出的图片存放在本地,发现只有在电脑联网的情况下才可以一下导出图片,后来查阅了一番资料,才发现highcharts中的导出默认的官网服务 ...

  4. 点点滴滴-NET下的常用框架

    刘冬的博客:http://www.cnblogs.com/GoodHelper/category/214139.html (Spring.net和Nhibernate) Kyo-yo  : http: ...

  5. webstore+nodejs

    新建一个普通的project. 编写如下代码: var http=require('http'); http.createServer(function(req,res){ res.writeHead ...

  6. php Magic methods __call

    <?php class Car{ protected $_color; protected $_model; public function __call($name,$arguments){ ...

  7. android 颜色收集及部分gridview样式设置

    < ?xml version="1.0" encoding="utf-8" ?> < resources> < color nam ...

  8. JNI编程(一) —— 编写一个最简单的JNI程序

    来自:http://chnic.iteye.com/blog/198745 忙了好一段时间,总算得了几天的空闲.貌似很久没更新blog了,实在罪过.其实之前一直想把JNI的相关东西整理一下的,就从今天 ...

  9. Form.KeyPreview 属性

    Form.KeyPreview 属性 今天再做KeyDown 和 KeyUp 事件时,就是忘了设置,窗体的KeyPreview 属性,所以KeyDown 和 KeyUp 事件没有反应(这里说明一下,本 ...

  10. Android开发UI之Notification

    Notification,顾名思义,通知,就是我们常说的系统推送. 官网链接:http://developer.android.com/reference/android/app/Notificati ...