Ultra-QuickSort
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 Sample Output 6 Source 题意:给定n个数,只能交换相邻的两个元素,至少交换几次,成为递增序列。
题解:
明显要求序列的逆序对数目。。。
对于样例:
5
9 1 0 5 4
我们将其排序:
0 1 4 5 9
在每个位置上初始放为1.
1 1 1 1 1
然后,从原序列开始遍历。
先到9,我们把其排好序的位置拿出,即为5。
然后统计位置5之前有多少1。
1 1 1 1 1
———— > 4个 ans+=4
然后把5号位置放为0。
1 1 1 1 0
继续操作即可。。。
这个树状数组维护即可。。。
注意开long long和原序列排序后要去重。。。
#include<bits/stdc++.h> |
相关文章
- POJ 2299 Ultra-QuickSort【树状数组 ,逆序数】
- P1774 最接近神的人_NOI导刊2010[树状数组 逆序对 离散化]
- POJ 3067 - Japan - [归并排序/树状数组(BIT)求逆序对]
- 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)
- POJ2299--树状数组求逆序数
- Ultra-QuickSort (求逆序数+离散化处理)、Cows、Stars【树状数组】
- poj 2299 树状数组求逆序对数+离散化
- Ultra-QuickSort poj2299 (归并排序 求逆序数对)
- codeforces 540E 离散化技巧+线段树/树状数组求逆序对
- POJ 2299树状数组求逆序对