Ultra-quicksort
Time Limit: 7000MS |
|
Memory Limit: 65536K |
Total Submissions: 46293 |
|
Accepted: 16846 |
Description
In this problem, you has to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping, 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 what many swap operations Ultra-quicksort needs to perform in order to sort a given input sequenc E.
Input
The input contains several test cases. Every test case begins with a line this contains a single integer n < 500,000-the length of the input sequence. Each of 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 is processed.
Output
For every input sequence, your program prints a single line containing an integer number OP, the minimum number of SWA P operations necessary to sort the given input sequence.
Sample Input
59105431230
Sample Output
60
Analysis:
We first enclose each value of the original array with an ordinal index and then sort it. Examples of topics such as:
Num:9 1 0 5 4
Index:1 2 3 4 5
After sorting:
Num:0 1 4 5 9
Index:3 2 5 4 1
Then, because the order num is 0 points in the 3rd of the original array, so in order to get it to the first, it will need to move forward two times, and it is equivalent to the smallest number 0 before the 2 number is larger than it (so to move two times), 0 moved to its own position, We will delete the 0 (with the aim of not having an effect on the back). Then look at the second largest number 1, which appears in the second of the original array, before he has a number larger than it so needs to be moved once. So the number of times that the 5 numbers need to be moved is:
Num:0 1 4 5 9
Count 2 1 2 1 0
The total number of times to add up is the number of times the final move is required.
Method Zhang Yi has already told me, but at first I always feel bad to achieve. Until later, slowly, slowly fix. method is to build a tree, instead of just putting the original num into the tree, it puts its subscript in the tree, initially assigning a value of 1 on each node. And then when the first num is found, because it is the position of the subscript 3, we are looking directly at the number of 1 between the interval [1,3]. This 1 is the number of the first num=0 to ask for the number of moves, and then we take 0 off, in fact, that is, the subscript 3 of the 1 removed. Each value is then calculated in turn.
Reference Code:
<span style= "font-family:kaiti_gb2312;" >/******************************************************************************* * Author:jinbao * Email: Dongjinbao9[email protected] * Last modified:2015-05-04 00:19 * Filename:poj2299.cpp * descrip tion: * *****************************************************************************/#include <iostream># Include <stdio.h> #include <algorithm>using namespace std;typedef long long ll;const int MAX = 500000+5;int tre E[4*max];struct array{int index;int value;} A[max];int n;bool cmp (array X,array y) {if (X.value<y.value) return True;return false;} void build (int node,int Begin,int end) {if (begin==end) Tree[node]=1;else{build (Node*2,begin, (begin+end)/2); Build ( Node*2+1, (begin+end)/2+1,end); tree[node]=tree[node*2]+tree[node*2+1];}} int query (int node,int begin,int end,int left,int right) {if (left>right) return 0;if (End<left | | right<begin) RET Urn 0;if (begin>=left && rigHt>=end) return Tree[node];return query (Node*2,begin, (begin+end)/2,left,right) +query (node*2+1, (begin+end)/2+1, End,left,right);} void update (int node,int begin,int end,int index,int value) {if (begin==end) {Tree[node]=value;return;} int mid= (begin+end)/2;if (mid>=index) update (node*2,begin,mid,index,value); Elseupdate (Node*2+1,mid+1,end,index , value); tree[node]=tree[node*2]+tree[node*2+1];} int main () {while (~SCANF ("%d", &n)) {if (n==0) break;for (int i=1;i<=n;i++) {scanf ("%d", &a[i].value); A[i]. Index=i;} Sort (a+1,a+n+1,cmp); build (1,1,n);; ll ans=0;for (int i=1;i<=n;i++) {ans+=query (1,1,n,1,a[i].index-1); update (1,1,n,a[i].index,0);} printf ("%lld\n", ans);} return 0;} </span>
poj2299 Ultra-quicksort (Segment tree count problem)