一、题意

给定数组,求交换几次相邻元素能是数组有序。

二、题解

刚开始以为是水题,心想这不就是简单的冒泡排序么。但是毫无疑问地超时了,因为题目中n<500000,而冒泡排序总的平均时间复杂度为,显然不满足要求。还有就是数组中的元素0
≤ a[i] ≤ 999,999,999,要用long long或_int64,在java中要用long。再一想用最快的快速排序,发现也不行。快排基于二分查找,交换次数很定要比题目要求的少。在想就是归并排序了,我们可以用归并排序求数组的逆序数。逆序数就等于冒泡排序的交换次数。

三、java代码

//Memory 9068K
//Time 2766MS
import java.util.Scanner; public class Main {
static int MAX =500010;
static int [] a=new int[MAX];
static int[] tb=new int[MAX/2];
static int [] te=new int[MAX/2];
static long sum;
private static void Merge(int[] a ,int beg,int mid,int end) {
int k;
int n1=mid-beg+1;
int n2=end-mid;
int i = 0, j = 0;
for(k=beg;k<=mid;k++){
tb[i++]=a[k];
}
tb[n1]=Integer.MAX_VALUE;
for(k=mid+1;k<=end;k++){
te[j++]=a[k];
}
te[n2]=Integer.MAX_VALUE;
i = j = 0; for(k=beg; k<=end; k++)
if( tb[i] <= te[j] )
a[k] = tb[i++];
else{
sum += ( n1 - i );
a[k] = te[j++];
}
} private static void MergeSort(int[] a,int beg,int end) {
if(beg<end){
int mid =(end+beg)/2;
MergeSort(a,beg,mid);
MergeSort(a,mid+1,end);
Merge(a,beg,mid,end);
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int n,i;
while((n=sc.nextInt())!=0){
sum=0;
for(i=0;i<n;i++){
a[i]=sc.nextInt();
}
MergeSort(a,0,n-1);
System.out.println(sum);
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Poj 2299 Ultra-QuickSort(归并排序求逆序数)的更多相关文章

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

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

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

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

  3. poj 2299 树状数组求逆序数+离散化

    http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...

  4. POJ 2299 -Ultra-QuickSort-树状数组求逆序数

    POJ 2299Ultra-QuickSort 使用树状数组记录逆序对数. 把数组按照大小顺序插入,getsum(i)就是i前面的比他大的数. #include <cstdio> #inc ...

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

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

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

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

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

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

  8. HDU 3743 Frosh Week(归并排序求逆序数)

    归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...

  9. hiho一下 第三十九周 归并排序求逆序数

    题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...

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

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

随机推荐

  1. java的小知识点

    1 获取当前路径 System.getProperty("user.dir") System.getProperty()参数大全# java.version            ...

  2. hdu 3718 Different Division

    Different Division Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. nc传文件

    nc传文件 先启动接收方 nc -l -p 9999 > index.lua 后启动发送方 nc 192.168.1.1 9999 < index.lua

  4. SVM学习笔记(一)

    支持向量机即Support Vector Machine,简称SVM.一听这个名字,就有眩晕的感觉.支持(Support).向量(Vector).机器(Machine),这三个毫无关联的词,硬生生地凑 ...

  5. QT设置textEdit光标到末尾

    //移动光标到末尾 QTextCursor cursor = ui->receiveTextEdit->textCursor(); cursor.movePosition(QTextCur ...

  6. 获取comboBox里面的item使用的方法

    使用currentIndex()或者currentText() void Widget::calc() { int first = ui->firstLineEdit->text().to ...

  7. python基础7 ---python函数

    python基础知识 一.闭包函数 1.闭包函数的定义:在一个内部函数中,在对外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包. 2.闭包函数的特点:自带作用域和延迟计算 补 ...

  8. java 类中定义接口的调用方法

    public class Human { public interface MyAction { public void getPower(); } } public class Test{ publ ...

  9. php正则表达式和数组

    一.正则表达式 1. “/”代表定界符,"^"代表起始符号,"$"代表结束符号 $str1="abc123def45ghjk6789lou" ...

  10. 【leetcode刷题笔记】Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...