Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 39397   Accepted: 14204

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

归并排序。

另外,此题有一坑就是结果会超int32;

详细能够參考:点击打开链接

我写的代码例如以下:

#include<cstdio>
#include<stdlib.h>
#include<cstring>
#include<algorithm>
#include<iostream> using namespace std; const int M = 500000 + 5;
int n, A[M], T[M], i; long long merge_sort(int l, int r, int *A)
{
if (r - l < 1) return 0;
int mid = (l + r) / 2;
long long ans = merge_sort(l, mid, A) + merge_sort(mid + 1, r, A);
i = l;
int p = l, q = mid + 1;
while (p <= mid && q <= r)
{
if(A[p] <= A[q])
T[i++] = A[p++];
else
{
ans += (mid + 1 - p);
T[i++] = A[q++];
}
}
while (p <= mid) T[i++] = A[p++];
while (q <= r) T[i++] = A[q++];
for (int j = l; j <= r; j++)
A[j] = T[j];
return ans;
} int main()
{
int n;
while(scanf("%d", &n) && n)
{
for(int j=0; j<n; j++)
scanf("%d", &A[j]);
printf("%lld\n", merge_sort(0, n - 1, A));
} return 0;
}

POJ 2299:Ultra-QuickSort的更多相关文章

  1. 树状数组求逆序对:POJ 2299、3067

    前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...

  2. 逆序数 POJ 2299 Ultra-QuickSort

    题目传送门 /* 题意:就是要求冒泡排序的交换次数. 逆序数:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序. 一个排列中逆序的总数就称为这个排列的逆 ...

  3. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  4. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  5. POJ 2299 Ultra-QuickSort(线段树+离散化)

    题目地址:POJ 2299 这题以前用归并排序做过.线段树加上离散化也能够做.一般线段树的话会超时. 这题的数字最大到10^10次方,显然太大,可是能够利用下标,下标总共仅仅有50w.能够从数字大的開 ...

  6. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  7. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

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

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

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

    点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34676   Accepted ...

随机推荐

  1. IOS开发-提升app性能的25条建议和技巧

    前言 这篇文章介绍了作者开发工作中总结的25个iOS开发tips, 多年之前读过这篇文章.收益良多,基本每一个tips在我的应用开发过程中都使用过.今天把这篇文章又一次整理转发下,与大家一起学习,不论 ...

  2. Ngxtop-Nginx日志实时分析利器

    ngxtop实时解析nginx访问日志,并且将处理结果输出到终端,功能类似于系统命令top,所以这个软件起名ngxtop.有了ngxtop,你可以实时了解到当前nginx的访问状况,再也不需要tail ...

  3. 在myeclipse中写sql语句的细节问题

    注意类型,varchar 和int  在java中表示为sql语句中的细微区别!! 下面的REGISEAT_NUM为int 类型       custid为varchar类型 String sql1= ...

  4. xpath语法速查

    xpath的具体学习可以通过w3c查看(链接:http://www.w3school.com.cn/xpath/index.asp) 这里只是将平时用到的几个表格贴出来,以后查询: 这里的xpath我 ...

  5. Android Wi-Fi Peer-to-Peer(Android的Wi-Fi P2P对等网络)

    Wi-Fi peer-to-peer(P2P,对等网络),它同意具备对应硬件的Android 4.0(API level 14)或者更高版本号的设备能够直接通过wifi而不须要其他中间中转节点就能直接 ...

  6. PHP-7

    PHP 7+ 版本极大地改进了性能,在一些WordPress基准测试当中,性能可以达到PHP 5.6的3倍. PHP 7+ 版本新加特性如下表所示: 序号 内容 1 PHP 标量类型与返回值类型声明 ...

  7. 14.怎样自学Struts2之Struts2类型转换[视频]

    14.怎样自学Struts2之Struts2类型转换[视频] 之前写了一篇"打算做一个视频教程探讨怎样自学计算机相关的技术",优酷上传不了.仅仅好传到百度云上: http://pa ...

  8. 使用autoconfig

    1. 增加maven 依赖 <properties> <maven.compiler.target>1.8</maven.compiler.target> < ...

  9. java 如何取前32位全是1的int型数据的后八位

    直接&255 因为Integer.toBinaryString(255) 是 8个1. 如果一个负数byte转成int则前面全部会补1,就是24个1和它自己的八位,,于是和八个1相&就 ...

  10. MassiGra045 简体中文化|打开图片很快

    MassiGra045 简体中文化,是一款对图片的打开预览很高效的工具,据传是日本开发的. 本人之前一直使用,唯一有点缺点就是不能旋转图片. 图片预览 峰回路转: http://pan.baidu.c ...