POJ——T 2299 Ultra-QuickSort

http://poj.org/problem?id=2299

Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 62894   Accepted: 23442

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

Waterloo local 2005.02.05

树状数组+离散化求逆序对

 1 #include <algorithm>
 2 #include <cstring>
 3 #include <cstdio>
 4
 5 using namespace std;
 6
 7 const int N(500000);
 8 long long ans;
 9 int n,tr[N];
10 struct Node
11 {
12     int num,mark;
13 }a[N];
14 bool cmp(Node a,Node b)
15 {
16     if(a.num==b.num)
17         return a.mark>b.mark;
18     return a.num>b.num;
19 }
20
21 #define lowbit(x) (x&((~x)+1))
22 inline void Update(int i,int x)
23 {
24     for(;i<=N;i+=lowbit(i)) tr[i]+=x;
25 }
26 inline int Query(int x)
27 {
28     int ret=0;
29     for(;x;x-=lowbit(x)) ret+=tr[x];
30     return ret;
31 }
32
33 int main()
34 {
35     for(;scanf("%d",&n)&&n;ans=0)
36     {
37         memset(tr,0,sizeof(tr));
38         for(int i=1;i<=n;i++)
39             scanf("%d",&a[i].num),a[i].mark=i;
40         sort(a+1,a+n+1,cmp);
41         for(int i=1;i<=n;Update(a[i++].mark,1))
42             ans+=(long long)Query(a[i].mark);
43         printf("%lld\n",ans);
44     }
45     return 0;
46 }
时间: 2024-10-13 16:51:12

POJ——T 2299 Ultra-QuickSort的相关文章

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

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 38688   Accepted: 13950 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

归并排序求逆序对模板(未完待续)

归并排序求逆序对题目(持续更新) \(1.\) \(Ultra\) \(Quicksort\) (需要该篇博文的阅读密码) 归并排序求逆序对 细节:传参三个,左.中.右三端点,每次运算注意中端点总取左右端点和的一半:返回条件为左右端点相等,此时无需排序. \(View\) \(Code\) void msort(int l,int mid,int r) { if(l==r) return; msort(l,(l+mid)>>1,mid); msort(mid+1,(r+mid+1)>&g

Poj 2299 Ultra-QuickSort

Problem地址:http://poj.org/problem?id=2299 这题题意:给一对序列,通过调换相邻元素位置的方法,将一组无序数据排序成递增有序数据. 这里有一个结论:通过调换相邻元素位置的方法,将一组无序数据排序成递增有序数据的最小调换次数为此组数据中逆序对的对数.这里不证明这个结论. 那么这题就变成求这组数据中逆序对的对数. 求逆序对的方法很多,我采用了基于归并排序的方法,参考自此网页:http://blog.csdn.net/morewindows/article/deta

poj 2299 Ultra-QuickSort 离散化 + 树状数组

题目链接:http://poj.org/problem?id=2299 离散化 + 树状数组 教科书例题般的题目 #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <vector> #include <stack> #include <set> #include

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

前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换相邻的两数使它有序,要你求出交换的次数.实质上就是求逆序对,网上有很多人说它的原理是冒泡排序,可以用归并排序来求出,但我一时间想不出它是如何和归并排序搭上边的(当初排序没学好啊~),只好用刚学过的树状数组来解决了.在POJ 1990中学到了如何在实际中应用上树状数组,没错,就是用个特殊的数组来记录即

POJ 2299 离散化线段树

点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 40827   Accepted: 14752 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by

POJ 2299 Ultra-QuickSort(归并排序求逆序对数)

题目地址:POJ 2299 今天下午的多校看来没有白做...实在做不出题闲着无聊看小白鼠学会了个归并排序.哈哈. 归并排序简单地说其实就是先分成一个二叉树直至单个,然后依次从最底层不断进行合并,逆序对数就是在合并的过程中,加入后面的那段中到了比他大的时候,那后面的那些就都是比他大的,都是逆序对数,所以直接加上即可.网上资料很多,就不细说了..用了分治的思想. 自己根据理解写的代码,考虑的太不全面了..又调了好长时间... 代码如下: #include <algorithm> #include

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

题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题思路: 根据冒泡排序的特点,我们可知,本题只需要统计每一个数的逆序数(如果有i<j,存在a[i] > a[j],则称a[i]与 a[j]为逆序数对),输出所有的数的逆序数的和用普通排序一定会超时,但是比较快的排序,像快排又无法统计 交换次数,这里就很好地体现了归并排序的优点.典型的利用归并排序求逆

poj 2299 Ultra-QuickSort (树状数组+离散化)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 48257   Accepted: 17610 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin