一、题意

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

二、题解

刚开始以为是水题,心想这不就是简单的冒泡排序么。但是毫无疑问地超时了,因为题目中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. 算法设计 mac 字符串 标识 n维度 2 3维度 字符串 标识值 特征值

    基向量

  2. 【python】-- socketserver

    socketserver SocketServer服务端内部使用 IO多路复用 以及 “多线程” 和 “多进程” ,从而实现并发处理多个客户端请求.即:每个客户端请求连接到服务器时,Socket服务端 ...

  3. Js编写的菜单树

    只需要提供这种JSON格式就ok了 其他的都可以直接引用这个代码进去 var testMenu=[ { "name": "一级菜单", "submen ...

  4. 中国移动OnetNet云平台 GET指令使用

    GET /devices//datastreams/KEY HTTP/1.1 Host: api.heclouds.com api-key: pmWLtnkDBSreKfvg7GsClUXdXa4A ...

  5. Ubuntu安装SSH + Windows上配置Putty

    1. Ubuntu安装SSH 命令: # sudo apt-get install openssh-server 2. 启动SSH Server 命令: # sudo /etc/init.d/ssh ...

  6. 模仿jquery框架源码---网络

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Page& ...

  7. php类和对象(一)

    对象:任何东西都可以称为对象,类实例化出来的东西类:对所有同类的对象抽象出来的东西 Info: Code,Name,Sex,Nation,Birthday对象:一条具体的信息 p001 张三 男 汉族 ...

  8. iOS 代码延迟执行

    1. [NSTread sleepForTimeInterval:0.8f] 这个方法 实际效果 好比打断点 等你再恢复断点执行 2.  [self performSelector:@selector ...

  9. AC自动机的一点理解

    \(fail\)指针:指向最长的在\(tire\)里出现的后缀 比\(tire\)多出来的子边:原来的\(tire\),我们失配后又得返回根结点再次匹配,而加入这些边后只需要花\(strlen(s)\ ...

  10. 字典树 HDU 1251 统计难题

    ;} 之前写的#include<iostream> #include<algorithm> #include<stdio.h> using namespace st ...