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. datatables的Bootstrap样式的分页怎么添加首页和尾页(引)

    找到dataTables.bootstrap.js(版本3):(此项目中文件名为:dataTableExt.js) $.fn.dataTableExt.oApi.fnPagingInfo = func ...

  2. python file operation

    file.open(name[,mode[,buffering]]) 模式的类型有: r 默认只读 w     以写方式打开,如果文件不存在则会先创建,如果文件存在则先把文件内容清空(truncate ...

  3. TKinter之输入框

    输入框是 Entry,应用程序要取得用户的信息,输入框是必不可少的. 输入框比较重要的一个函数: get:返回值即输入框的内容 比如e是一个输入框,e['show']='*'就变成了密码框 小例子:用 ...

  4. kettle常见问题解决

    开源ETL工具kettle系列之常见问题 摘要:本文主要介绍使用kettle设计一些ETL任务时一些常见问题,这些问题大部分都不在官方FAQ上,你可以在kettle的论坛上找到一些问题的答案 1. J ...

  5. 配置Windows Server2008+iis+php+mysql所需下载安装包

    最近一个朋友让我帮忙给配置服务器iis+php+mysq 环境,遇到了很多问题,特此就在这里说一下.小弟只是在windwos2003 和windwos XP下配置过iis+php+mysql,去朋友那 ...

  6. php 连接测试sphinx

    shpinx.php <?php header("Content-type:text/html;charset=utf-8"); include 'SphinxClient. ...

  7. UIDatePicker自定义背景

    selectDatePicker = [[UIDatePicker alloc]init];    selectDatePicker.frame = CGRectMake(0, 10, 280, 21 ...

  8. Java-Lambda

    1. 函数式接口 函数式接口可以包含多个默认方法.类方法,但是只能有一个抽象方法. Lambda表达式的目标类型是函数式接口. java.util.function包下,定义了大量的函数式接口 2. ...

  9. php正则替换所有空格和换行

    替换所有空格为空 $contents=" abc "; $contents=preg_replace('/\s+/','',$contents); //结果$contents=&q ...

  10. 如何添加WebService调用时的用户认证

    场景: 当把发布好的WebService地址或WSDL提供给调用方时,需要对方先进行身份的认证通过后才允许接口的进步访问.而不是公开的谁都可以调用. 解决: 1.在IIS中设置对应网站的目录访问权限. ...