Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 34470   Accepted: 12382

题目链接:http://poj.org/problem?id=2299

Description

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 
9 1 0 5 4 ,
Ultra-QuickSort produces the output 
0 1 4 5 9 .
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0

Source

 
题目大意:给出一段数字序列,每次只能交换相邻的两个数,问最少需要多少次可以使得这段序列变成顺序序列。
思路:用冒泡排序超时,因为给的数据量很大。用归并排序的方法可以求出逆序数,在这里,逆序数就是冒泡排序的交换次数,所以这道题目就是用归并排序的方法对数组进行排序,并得出逆序数,输出即可。
代码:
 #include<iostream>
#include<string.h>
#include<cstdio>
#include<cstdlib>
#define max 1000000
using namespace std;
int n,f[max],g[max];
long long int sum=;
void guibing(int l,int mid,int r)
{
int t=;
int i=l,j=mid+;
while(i<=mid&&j<=r)
{
if(f[i]<=f[j])
{
g[t++]=f[i];
i++;
}
else
{
g[t++]=f[j];
j++;
sum=sum+mid-i+;
}
}
while(i<=mid)g[t++]=f[i++];
while(j<=r)g[t++]=f[j++];
for(i=;i<t;i++)
f[l+i]=g[i];
}
void quicksort(int l,int r)
{
if(l<r)
{
int mid=(l+r)/;
quicksort(l,mid);
quicksort(mid+,r);
guibing(l,mid,r);
}
}
int main()
{
while()
{
cin>>n;
if(n==)break;
int i;
sum=;
for(i=;i<=n-;i++)
cin>>f[i];
quicksort(,n-);
cout<<sum<<endl;
}
return ;
}

Ultra-QuickSort【归并排序典型题目】的更多相关文章

  1. MySQL基础练习---牛客网的数据以及典型题目

    1 部门表departments 部门no和部门名称 2 部门员工表 dept_emp 每个部门对应的员工信息 3 部门经理表 dept_manager 每个部门的经理信息 4 员工表 employe ...

  2. (PMP)解题技巧和典型题目分析(每日20题)

    3.11 1.A(C),2.D,3.A,4.B,5.A(C),6.D(A),7.D,8.A(D),9.B,10.D(B), 11.C(B),12.C(D),13.B,14.D,15.C,16.C(D) ...

  3. (PMP)解题技巧和典型题目分析(模拟二)

  4. (PMP)解题技巧和典型题目分析(模拟一)

  5. (PMP)解题技巧和典型题目分析(0903-3班)

    B.项目有依赖 D A A B B C B C D B A B B A B

  6. (PMP)解题技巧和典型题目分析(0903-2班)

    1.计算题 ,5 2.概念题,少 3.情景题,很多 C B C D ------------------------------------------------------------------ ...

  7. MySQL索引面试题分析(索引分析,典型题目案例)

    [建表语句] create table test03( id int primary key not null auto_increment, c1 char(10), c2 char(10), c3 ...

  8. leetcode数组典型题目小结

    数组与矩阵 数组与矩阵的基本知识: 1.数组:数组是在程序设计中,为了处理方便, 把具有相同类型的若干元素按有序的形式组织起来的一种形式. 首先,数组会利用索引来记录每个元素在数组中的位置,且在大多数 ...

  9. LeetCode:链表专题

    链表专题 参考了力扣加加对与链表专题的讲解,刷了些 leetcode 题,在此做一些记录,不然没几天就没印象了 出处:力扣加加-链表专题 总结 leetcode 中对于链表的定义 // 定义方式1: ...

随机推荐

  1. linux 文件系统sysvinit 流程分析

    参考网上许多的教程. 然后有一下相关的笔记: kernel 在挂载完文件系统后,会执行第一个进程init 这个进程的PID为1 这个进程是所有进程的父进程 init 进程,首先要去读取inittab中 ...

  2. js之词法分析

    词法分析 词法分析: 先分析参数: 再分析变量声明: 分析函数声明: 一个函数能使用的局部变量,就从上面的3步分析而来 具体步骤: 1.函数运行前的一瞬间,生成 Active Object(活动对象) ...

  3. Linux下端口被占用解决

      有时候关闭软件后,后台进程死掉,导致端口被占用.下面以JBoss端口8083被占用为例,列出详细解决过程. 解决方法: 1.查找被占用的端口 netstat -tln netstat -tln | ...

  4. jquery为新增元素添加事件

    <script type="text/javascript"> var $id=1; $(function(){ $(".hehe").click( ...

  5. apache2错误日志在哪,可以看到php错误

    在以下路径中 /var/log/apache2/error.log

  6. MySQL一次插入多行数据

    CREATE TABLE `viewhistory` ( `viewid` ) NOT NULL AUTO_INCREMENT, `uid` ) NOT NULL, `video` ) NOT NUL ...

  7. iOS 中 为UIView添加背景图片

    创建UIImage的方法有两种: UIImage *image = [UIImageimageNamed:@"image.jpg"];//这种不释放内存,要缓存 NSString ...

  8. IN和EXISTS的详解

    从效率来看: 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; T1数据量小而T2数据量非常大时,T1<& ...

  9. jq鼠标隐藏显示的方法

    <div style="width:300px; float:left;">        <div onmouseover="testOnmouseO ...

  10. 数位DP题目汇总

    Google Code Jam 2014 Round 1B Problem B hdu 2089 hdu 3555 uestc 250 (原1307) hdu 3652 hdu 3709 Light ...