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. windows添加linux 启动引导项

    需要软件: EasyBCD 下载地址: http://pan.baidu.com/s/1c20v1GO 在windows 下安装上述软件. 然后打开,语言的话现在中文即可. 可以看到如下画面: 1.选 ...

  2. Ngrok搭建服务器

    一.ngrok简介及作用 ngrok 是一款用go语言开发的开源软件,它是一个反向代理,通过在公共的端点和本地运行的 Web 服务器之间建立一个安全的通道.下图简述了ngrok的原理. ngrok 可 ...

  3. Implement a TextView with an animation in its left side

    In my case, I want to write a TextView with an animation in its left side. ImageView + TextView coul ...

  4. AJAX 页面数据传递

    $.ajax({ //一个Ajax过程 type: "post", //以post方式与后台沟通 url: "personstockajax.php", //与 ...

  5. ubuntu下配置apache2多域名(apache2.4.6)

    Ubuntu 在 Linux 各发行版中, 个人用户数量最多的. 很多人在本机和虚拟机中使用. 但 Ubuntu 和 Redhat 的 VirtualHost 设置方法不相同. 1. 打开目录 /et ...

  6. 转:shell杀死指定名称的进程

    #!/bin/sh #根据进程名杀死进程 ] then echo "缺少参数:procedure_name" exit fi PROCESS=`|grep -v grep|grep ...

  7. C语言也能干大事1

    今天看了个视频,叫C语言也能干大事,写了第一个WIN项目的代码,感觉特别好,就像以前刚刚学会写C语言一样, 然后就恶搞出一个东西,最后的结果就是这个东西退出不了了

  8. Storm集群的安装配置

    Storm集群的安装分为以下几步: 1.首先保证Zookeeper集群服务的正常运行以及必要组件的正确安装 2.释放压缩包 3.修改storm.yaml添加集群配置信息 4.使用storm脚本启动相应 ...

  9. 1.nodejs权威指南--基础知识

    1. 基础知识 1.1 全局作用域及函数 1.1.1 全局作用域 在nodejs中,定义了一个global对象,代表nodejs中的全局命名空间,任何全局变量.函数或对象都是该对象的一个属性值 1.1 ...

  10. 针对跑MySQL的Linux优化【转】

    本文来自:http://www.mysqlsupport.cn/linux-performance-tuning-tips-mysql/ 因为很多MySQL的生产环境都在Linux下,我决定指出一些L ...