Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 34676   Accepted: 12465

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

题目意图很明确了,就是给你一些数,求逆序数,但是数据量很大,普通的n^2求逆序数的方法铁定超时,所以只能用归并排序求逆序数,合并的时候,我们假设两部分为part1和part2,(part1在前,part2在后)这两部分已经排好序了,那么合并part1和part2的时候,如果part1的top1位置的数大于part2的top2位置的数,那么说明part1后面的那些数也都要比part2的top2位置的数大,所以逆序数就是mid到part1位置的距离

#include<stdio.h>
#include<iostream>
using namespace std; int array[5000001];
__int64 flag = 0; void merg(int head, int tail)
{
int mid = (tail + head) / 2 + 1;
int * new_array = new int[(tail - head) + 1];
int top1 = head;
int top2 = mid;
int i;
for(i = 0; top1 < mid && top2 <= tail ; i++)
{
if(array[top1] > array[top2])
{
new_array[i] = array[top2];
top2 ++;
}
else
{
new_array[i] = array[top1];
flag += top2 - (mid);
top1 ++;
}
}
if(top1 == mid && top2 <= tail)
{
while(top2 <= tail)
new_array[i++] = array[top2++];
}
else if(top1 != mid && top2 > tail)
{
while(top1 < mid)
{
new_array[i++] = array[top1++];
flag += tail - (mid) + 1;
}
}
memcpy(&array[head], new_array, sizeof(int) * (tail - head + 1) );
}
void mergsort(int head, int tail)
{
if(head >= tail)
return ;
mergsort(head, (head + tail) / 2);
mergsort((head + tail) / 2 + 1, tail);
merg(head, tail);
}
int main()
{
int n;
while(scanf("%d", &n), n != 0)
{
int i;
flag = 0;
for(i = 0; i < n; i++)
scanf("%d", &array[i]); mergsort(0, n - 1);
printf("%I64d\n", flag); }
return 0;
}

poj 2299 Ultra-QuickSort :归并排序求逆序数的更多相关文章

  1. 题解报告:poj 2299 Ultra-QuickSort(BIT求逆序数)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  2. poj 2299 树状数组求逆序数+离散化

    http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...

  3. POJ 2299 -Ultra-QuickSort-树状数组求逆序数

    POJ 2299Ultra-QuickSort 使用树状数组记录逆序对数. 把数组按照大小顺序插入,getsum(i)就是i前面的比他大的数. #include <cstdio> #inc ...

  4. poj 2299 Ultra-QuickSort 归并排序求逆序数对

    题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...

  5. [CF 351B]Jeff and Furik[归并排序求逆序数]

    题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...

  6. POJ2299 Ultra-QuickSort(归并排序求逆序数)

    归并排序求逆序数   Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  7. HDU 3743 Frosh Week(归并排序求逆序数)

    归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...

  8. hiho一下 第三十九周 归并排序求逆序数

    题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...

  9. poj 2299 Ultra-QuickSort (归并排序 求逆序数)

    题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...

随机推荐

  1. 【转】PHP简单拦截器的实现

    最近在看Yii的源代码,收获了不少,这里就是从中得到的启发,而写的一个简单拦截器的实现下面看例子: <?phpclass A{    private $_e = array();       p ...

  2. 更简单的跨域解决方案 - CORS

    跨域问题是前端开发经常遇到的了,大家可能常用的就是JSONP了, JSONP非常方便,只要前后端约定好一个方法名,就可以沟通了,但JSONP也有一定的局限,JSONP只支持GET请求,还有当你想提供一 ...

  3. 基于LBS的地理位置附近的搜索以及由近及远的排序

    Nosql学习之Redis资料(一) http://redis.io/download 目前基于LBS地理位置的搜索已经应用非常广了,的确是个很方便的东西. 我们做程序的就是要考虑如何通过这些功能,来 ...

  4. 【Reporting Services 报表开发】— 级联式参数设置

    级联式参数设置 再清楚的菜单,只要遇到选择项目一多的时候,难免会让人眼花缭乱,而找不到该选的选项.举例来说,像是零售业动辄万种商品品类,如果希望快速的选择到希望查看的产品品类时,就需要更有效率的搜索方 ...

  5. linux jdk+mysql+tomcat+nginx 项目部署步骤

    1.下载linux jdk1.7.0_79.tar.gz ; 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-dow ...

  6. 查看SQL执行计划

    一用户进入某界面慢得要死,查看SQL执行计划如下(具体SQL语句就不完全公布了,截断的如下): call     count       cpu    elapsed       disk       ...

  7. 别样的checkbox

    <style type="text/css"> input[type=checkbox] { visibility: hidden; } .slide_check_bo ...

  8. AS3 编码解码函数 特殊字符转义

    有时候传输特殊字符的时候,需要将字符转义, trace(escape("!@#$%^&*()_+<>?'"));//输出:%21@%23%24%25%5E%26 ...

  9. Silverlight开源框架SL提供便捷的二次开发银光框架

    Silverlight开发框架SilverFrame欢迎咨询 基于Silverlight4.0开发,兼容Silverlight 5.0,SQLServer2005数据库.WCF: 本框架有清爽的前端界 ...

  10. 【转】class卸载、热替换和Tomcat的热部署的分析

    这篇文章主要是分析Tomcat中关于热部署和JSP更新替换的原理,在此之前先介绍class的热替换和class的卸载的原理.一 class的热替换ClassLoader中重要的方法 loadClass ...