POJ2299: Ultra-QuickSort-合并排序解决逆序数问题
#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-合并排序解决逆序数问题的更多相关文章
- poj2299解题报告(归并排序求逆序数)
POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...
- poj2299--归并排序求逆序数
/** \brief poj2299 * * \param date 2014/8/5 * \param state AC * \return memory 4640K time 3250ms ...
- [CF 351B]Jeff and Furik[归并排序求逆序数]
题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...
- 线段树求逆序数方法 HDU1394&&POJ2299
为什么线段树能够求逆序数? 给一个简单的序列 9 5 3 他的逆序数是3 首先要求一个逆序数有两种方式:能够从头開始往后找比当前元素小的值,也能够从后往前找比当前元素大的值,有几个逆序数就是几. 线段 ...
- nyoj 117 求逆序数 (归并(merge)排序)
求逆序数 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中 ...
- poj 1007:DNA Sorting(水题,字符串逆序数排序)
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 80832 Accepted: 32533 Des ...
- POJ2299 Ultra-QuickSort(归并排序求逆序数)
归并排序求逆序数 Time Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- hdu 1394 zoj 1484 求旋转序列的逆序数(并归排序)
题意:给出一序列,你可以循环移动它(就是把后面的一段移动到前面),问可以移动的并产生的最小逆序数. 求逆序可以用并归排序,复杂度为O(nlogn),但是如果每移动一次就求一次的话肯定会超时,网上题解都 ...
- SDUT 3402 数据结构实验之排序五:归并求逆序数
数据结构实验之排序五:归并求逆序数 Time Limit: 40MS Memory Limit: 65536KB Submit Statistic Problem Description 对于数列a1 ...
随机推荐
- 【mapping】 springmvc的注解mapping无法生效的问题
springmvc 始终无法加载 注解 map, 解决办法 八月 11, 2015 8:24:42 下午 org.springframework.web.servlet.DispatcherServl ...
- STL set_difference set_intersection set_union 操作
以下是STL algorithm的几个函数,使用的条件是有序容器,所以 vector在被sort了之后是可以使用的,set也是可以使用的. set_difference 这个是求得在第一个容器中有,第 ...
- C#中如何按字节数截取字符串?
http://www.cnblogs.com/xuejie/archive/2012/12/14/2818452.html
- shell脚本中>/dev/null的含义
shell脚本中>/dev/null的含义 shell脚本中有段使用sqlplus的部分: sqlplus ${user_id} <<! >/dev/null spool sp ...
- javascript-代码复用模式
代码复用模式 1)使用原型继承 函数对象中自身声明的方法和属性与prototype声名的对象有什么不同: 自身声明的方法和属性是静态的, 也就是说你在声明后,试图再去增 ...
- 1012: [JSOI2008]最大数maxnumber
单点更新,区间求最大值的题: 可以使用树状数组和线段树: #include<cstdio> #include<cstring> #include<algorithm> ...
- ****Objective-C 中的方法的调用
oc语言中采用特定的语言调用类或者实例(对象)的方法称为发送消息或者方法调用. oc中方法的调用有两种: 第一种: [类名或对象名 方法名]; [ClassOrInstance method]; [C ...
- 【Lucene3.6.2入门系列】第03节_简述Lucene中常见的搜索功能
package com.jadyer.lucene; import java.io.File; import java.io.IOException; import java.text.SimpleD ...
- jstring 和char 之间的转换方法
//jstring to char* char* jstringTostring(JNIEnv* env, jstring jstr) { char* rtn = NULL; jclass clsst ...
- 【DataStructure In Python】Python模拟二叉树
使用Python模拟二叉树的基本操作,感觉写起来很别扭.最近做编译的优化,觉得拓扑排序这种东西比较强多.近期刷ACM,发现STL不会用实在太伤了.决定花点儿时间学习一下STL.Boost其实也很强大. ...