Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 48111   Accepted: 17549

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 题意就是求所给序列的逆序数
可以用归并排序求
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define WW freopen("output.txt","w",stdout)
#define RR freopen("input.txt","r",stdin) using namespace std; const int MAX= 500100 ; int a[MAX];
int b[MAX];
int n;
LL sum;//开始估计错值,用int 直接爆掉了
void Link_Sqrt(int low,int mid,int Low,int high)
{
int i=low,j=Low,s=0;
while(i<=mid&&j<=high)
{
while(a[i]<=a[j]&&i<=mid&&j<=high)
{
b[s++]=a[i++];
}
while(a[j]<a[i]&&i<=mid&&j<=high)//当a[i]>a[j]时,因为a[i--mid]与a[mid=1--high]都是有序序列,所以对与a[j]他的逆序数就是mid-i+1
{
sum+=(mid-i+1);
b[s++]=a[j++];
}
}
while(i<=mid)
{
b[s++]=a[i++];
}
while(j<=high)
{
b[s++]=a[j++];
}
for(int k=0;k<s;k++)
{
a[low+k]=b[k];
}
}
void Sqrt(int low,int high)
{
int i=low,j=high;
if(i>=j)
{
return ;
}
int mid=(i+j)/2;//二分
Sqrt(i,mid);
Sqrt(mid+1,j);
Link_Sqrt(i,mid,mid+1,j);//归并
} int main()
{
while(scanf("%d",&n),n)
{
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sum=0;
Sqrt(0,n-1);
printf("%lld\n",sum);
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Ultra-QuickSort 分类: POJ 排序 2015-08-03 15:39 2人阅读 评论(0) 收藏的更多相关文章

  1. 8大排序算法图文讲解 分类: B10_计算机基础 2014-08-18 15:36 243人阅读 评论(0) 收藏

    排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...

  2. DZY Loves Chemistry 分类: CF 比赛 图论 2015-08-08 15:51 3人阅读 评论(0) 收藏

    DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. UI基础:UIButton.UIimage 分类: iOS学习-UI 2015-07-01 21:39 85人阅读 评论(0) 收藏

    UIButton是ios中用来响应用户点击事件的控件.继承自UIControl 1.创建控件 UIButton *button=[UIButton buttonWithType:UIButtonTyp ...

  4. javascript中0级DOM和2级DOM事件模型浅析 分类: C1_HTML/JS/JQUERY 2014-08-06 15:22 253人阅读 评论(0) 收藏

    Javascript程序使用的是事件驱动的设计模式,为一个元素添加事件监听函数,当这个元素的相应事件被触发那么其添加的事件监听函数就被调用: <input type="button&q ...

  5. XHTML 结构化:使用 XHTML 重构网站 分类: C1_HTML/JS/JQUERY 2014-07-31 15:58 249人阅读 评论(0) 收藏

    http://www.w3school.com.cn/xhtml/xhtml_structural_01.asp 我们曾经为本节撰写的标题是:"XHTML : 简单的规则,容易的方针.&qu ...

  6. 各种排序算法的分析及java实现 分类: B10_计算机基础 2015-02-03 20:09 186人阅读 评论(0) 收藏

    转载自:http://www.cnblogs.com/liuling/p/2013-7-24-01.html 另可参考:http://gengning938.blog.163.com/blog/sta ...

  7. Hadoop常见异常及其解决方案 分类: A1_HADOOP 2014-07-09 15:02 4187人阅读 评论(0) 收藏

    1.Shell$ExitCodeException 现象:运行hadoop job时出现如下异常: 14/07/09 14:42:50 INFO mapreduce.Job: Task Id : at ...

  8. Basic 分类: POJ 2015-08-03 15:49 3人阅读 评论(0) 收藏

    Basic Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 905 Accepted: 228 Description The p ...

  9. 选择排序 分类: 算法 c/c++ 2014-10-10 20:32 509人阅读 评论(0) 收藏

    选择排序(假设递增排序) 每次选取从当前结点到末尾结点中最小的一个与当前结点交换,每一轮固定一个元素位置. 时间复杂度O(n^2),空间复杂度O(1).下面的示例代码以带头结点的链表为存储结构: #i ...

随机推荐

  1. Redis:安装

    此篇文章并不介绍linux下安装方法.围绕windows安装而介绍 两种安装方式: 1)下载压缩包,解压后(运行起来有个窗口,关闭掉就不在运行),没有windows服务被注册:可以只用命令在cmd将其 ...

  2. 分子模拟软件Schrodinger Suites 2015安装

    安装平台:redhat 5.6 schrodinger 2015 先把schrodinger_2015.iso 挂载到/mnt >>>> mount -o loop schro ...

  3. Lintcode: Recover Rotated Sorted Array

    Given a rotated sorted array, recover it to sorted array in-place. Example [4, 5, 1, 2, 3] -> [1, ...

  4. 转:Beautiful Soup

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时 ...

  5. How to create a project with existing folder of files in Visual Studio?

    1. Select Visual Studio tool bar-> New -> Project from existing code-> continue with config ...

  6. HDU 4897 Little Devil I(树链剖分)(2014 Multi-University Training Contest 4)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4897 Problem Description There is an old country and ...

  7. paper 93:OpenCV学习笔记大集锦

    整理了我所了解的有关OpenCV的学习笔记.原理分析.使用例程等相关的博文.排序不分先后,随机整理的.如果有好的资源,也欢迎介绍和分享. 1:OpenCV学习笔记 作者:CSDN数量:55篇博文网址: ...

  8. ppaer 67 : matlab 函数errorbar

    MATLAB ERRORBAR 这个函数的意思是:ERRORBAR(X,Y,L,U),X是自变量,Y是因变量,L是Y的变动下限,U是Y的变动上限 errorbar(X,Y,E)  X是自变量,Y是因变 ...

  9. oracle热备份

    1:热备份: SHUTDOWN IMMEDIATE; STARTUP MOUNT; alter database archivelog; --ALTER SYSTEM SET LOG_ARCHIVE_ ...

  10. JS中数组的操作

    1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ...