POJ2299+逆序数
归并排序!!!!!!!!!!
/*
归并排序+求逆序数
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;
typedef __int64 int64;
const int maxn = ;
int64 a[ maxn ],Sort[ maxn ];
int64 res; void init(){
res = ;
} void merge( int L,int R ){
int mid = (L+R)/;
int i = L;
int j = mid+;
int pos = L;
while( i<=mid&&j<=R ){
if( a[i]<a[j] )
Sort[ pos++ ] = a[i++];
else{
Sort[ pos++ ] = a[j++];
res += (mid-i+);
}
}
while( i<=mid )
Sort[ pos++ ] = a[i++];
while( j<=R )
Sort[ pos++ ] = a[j++];
for( int k=L;k<=R;k++ )
a[k] = Sort[k];
} void merge_sort( int L,int R ){
int mid = (L+R)/;
if( L<R ){
merge_sort( L,mid );
merge_sort( mid+,R );
merge( L,R );
}
return ;
} int main(){
int n;
while( scanf("%d",&n),n ){
for( int i=;i<=n;i++ )
scanf("%I64d",&a[i]);
init();
merge_sort( ,n );
printf("%I64d\n",res);
}
return ;
}
POJ2299+逆序数的更多相关文章
- poj2299解题报告(归并排序求逆序数)
POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...
- POJ2299 Ultra-QuickSort(归并排序求逆序数)
归并排序求逆序数 Time Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- 线段树求逆序数方法 HDU1394&&POJ2299
为什么线段树能够求逆序数? 给一个简单的序列 9 5 3 他的逆序数是3 首先要求一个逆序数有两种方式:能够从头開始往后找比当前元素小的值,也能够从后往前找比当前元素大的值,有几个逆序数就是几. 线段 ...
- poj2299--归并排序求逆序数
/** \brief poj2299 * * \param date 2014/8/5 * \param state AC * \return memory 4640K time 3250ms ...
- HDU3465 树状数组逆序数
Life is a Line Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)T ...
- HDU 1394 Minimum Inversion Number(最小逆序数 线段树)
Minimum Inversion Number [题目链接]Minimum Inversion Number [题目类型]最小逆序数 线段树 &题意: 求一个数列经过n次变换得到的数列其中的 ...
- 递归O(NlgN)求解逆序数
导言 第一次了解到逆序数是在高等代数课程上.当时想计算一个数列的逆序数直觉就是用两重循环O(n^2)暴力求解.现在渐渐对归并算法有了一定的认识,因此决定自己用C++代码小试牛刀. 逆序数简介 由自然数 ...
- FZU 2184 逆序数还原
传送门 Description 有一段时间Eric对逆序数充满了兴趣,于是他开始求解许多数列的逆序数(对于由1...n构成的一种排列数组a,逆序数即为满足i<j,ai>aj的数字对数),但 ...
- HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description The inve ...
随机推荐
- form表单按enter键自动提交的问题
废话不多说.直接上代码. 1:form表单按enter键自动提交的情况 <!doctype html> <html lang="en"> <head& ...
- 收藏2个mongodb connector网址
https://github.com/plaa/mongo-spark https://github.com/mongodb/mongo-hadoop http://codeforhire.com/2 ...
- Android richtext
在项目开发过程中经常会遇到很多需要显示不同样式的,不同风格的文本信息:对此可以使用多个TextView来分别设置自已想要的样式以满足需求,但是使用多个TextView的方式不太好:使用多个TextVi ...
- 如何在ANDROID JNI 的C++中打Log
http://blog.csdn.net/pkigavin/article/details/8583537 最近在研究Android 2.3.3源代码的C/C++层,需要对代码进行一些调试,但是奇怪的 ...
- Java多线程编程总结(学习博客)
Java多线程编程总结:网址:http://lavasoft.blog.51cto.com/62575/27069/
- 【JAVA】浅谈java枚举类
一.什么情况下使用枚举类? 有的时候一个类的对象是有限且固定的,这种情况下我们使用枚举类就比较方便? 二.为什么不用静态常量来替代枚举类呢? public static final int SEASO ...
- ### CUDA
CUDA Learning. #@author: gr #@date: 2014-04-06 #@email: forgerui@gmail.com 1. Introduction CPU和GPU的区 ...
- 373. Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. 给你两个数组n ...
- Mac:How to mount a Windows shared folder
Reference: How to mount a Windows shared folder on your Mac
- 为UITextView添加与UITextField一样的边框——UITextField默认边框颜色、宽度、圆角
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3789052.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...