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. android中paint的setXfermode属性

    本文前半部分来自于:http://www.cnblogs.com/rayray/p/3670120.html 1.下面的Xfermode子类可以改变这种行为: AvoidXfermode  指定了一个 ...

  2. BZOJ 2448: 挖油

    Description [0,x]中全是1,其余全是0,每个点有一个权值,求最坏情况下得到x的最小权值. Sol DP+单调队列. 首先就是一个 \(O(n^3)\) 的DP. \(f[i][j]\) ...

  3. Python super继承详解

    MRO(Method resolution order)是python用来解析方法调用顺序的,mro中记录了一个类的所有基类的类类型序列,super不是简单地调用基类的方法,而是按照MRO中的顺序来调 ...

  4. Linux的目录结构

    学习Linux这么久,对Linux的目录的目录结构进行整理总结一下. 以下是对这些目录的解释: /bin:bin是Binary的缩写, 这个目录存放着最经常使用的命令. /boot:这里存放的是启动L ...

  5. django的前后的结合,search搜索功能案例

    利用django的Q()功能可以很好的展开搜索功能 假设我要做个这样的搜索功能

  6. centos终端中mysql中文显示乱码的处理

    http://stackoverflow.com/questions/3513773/change-mysql-default-character-set-to-utf-8-in-my-cnfhttp ...

  7. LINQ查询字符串判断是否大写

    #region Linq to 字符串char.IsUpper意思是判断是否大写            //string strDemo = "HelloWord!";       ...

  8. POJ 1065

    http://poj.org/problem?id=1065 题目的大体意思就是给一些木头长l,重w,有一个机器,如果切割这些木头的话,在i后面切割的i+1根木头满足长度和重量都要大于等于前一根则不需 ...

  9. vim python设置

    http://www.cnblogs.com/Leo-Forest/archive/2012/04/06/2435237.html http://linux-wiki.cn/wiki/zh-hans/ ...

  10. 使用Java中File类批量创建文件和批量修改文件名

    批量创建文件 int cont = 1; String s = "E:\\学习资料\\Java笔记-"; while(cont<100){ File f = new File ...