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. PHP微信支付开发实例

    这篇文章主要为大家详细介绍了PHP微信支付开发过程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 PHP微信支付开发过程,分享给大家,供大家参考,具体内容如下 1.开发环境 Thinkphp 3. ...

  2. HDU 2222 ----AC自动机

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  3. Windows 2008R2关闭网络发现

    在Windows Server 2008 R2安装完后,默认情况下,在高级共享设置中无法对网络发现的更改进行保存(每次选择"启用网络发现"后保存修改,但重新打开"高级共享 ...

  4. PHP检测每一段代码执行时间

    <?php // 实例1 /** * @start time */ function proStartTime() { global $startTime; $mtime1 = explode( ...

  5. 《oracle每天一练》Oracle之物化视图

    相关帖子思考和跟踪 本文转自Ronger 物化视图是一种特殊的物理表,“物化”(Materialized)视图是相对普通视图而言的.普通视图是虚拟表,应用的局限性大,任何对视图的查询,Oracle都实 ...

  6. POJ 1661

    http://poj.org/problem?id=1661 这是一道DP的题目,求最优解 上面的这一个题是对于那个重左边开始上的函数的解释 题目要求的是从最高掉下来的小时间,那么我们就可以求从最低处 ...

  7. Light OJ 1032

    数位dp,许多数位dp需要统计某种模式(子串)出现的数量,这种题通常需要在递归参数中加入高位已经出现过的模式的数量. #include <cstdio> #include <cstr ...

  8. 构建web应用示例

    1.1 请求方法的判断 var http = require('http'); var server = http.createServer(function(request,response){ s ...

  9. c#excel的操作例子

    class MyData//存储行数据 { public List<string> RowData { get; set; } } static void Main(string[] ar ...

  10. static 修饰内部类

    static一般用来修饰成员变量或函数也修饰代码块,一般不能修饰类,但是可以修饰内部类,被修饰的内部类可以直接作为一个普通类来用,不需要创建一个外部类的实例,而普通内部类的引用需要创建一个外部类的实例 ...