Ultra-quicksort
Time Limit: 7000MS |
|
Memory Limit: 65536K |
Total Submissions: 55048 |
|
Accepted: 20256 |
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 swap op Erations necessary to sort the given input sequence.
Sample Input
59105431230
Sample Output
60
Test instructions: is to seek the number of reverse order
Analysis: There are several ways to use two of
#include <stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespacestd;Const intMAXN =500010;intF[MAXN];intA[MAXN];intN;pair<int,int>P[MAXN];voidUpdateintXintval) { while(x<=N) {f[x]+=Val; X+=x&-x; }}intQueryintx) { intres=0; while(x>0) {res+=F[x]; X-=x&-x; } returnRes;}intMain () { while(SCANF ("%d", &n) = =1&&N) {memset (F,0,sizeof(F)); Memset (A,0,sizeof(a)); for(intI=1; i<=n;i++) {scanf ("%d",&P[i].first); P[i].second=i; } sort (P+1, p+n+1); a[p[1].second]=1; for(intI=2; i<=n;i++) { if(p[i].first!=p[i-1].first) a[p[i].second]=i; Elsea[p[i].second]=a[p[i-1].second]; } Long Longans=0; for(intI=1; i<=n;i++) {update (a[i],1); Ans+=query (N)-query (A[i]); } printf ("%lld\n", ans); } return 0;}
/*using merge sort to find reverse order number*/#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespacestd;Const intMAXN =500010;intA[MAXN],B[MAXN],C[MAXN];Long Longans;//Merge the l-mid,mid+1-r that are already lined upvoidMergeint*a,intLintMidintR) { for(intI=0, j=l;j<=mid;j++) B[i++]=A[j]; intlen1=mid-l+1; for(intI=0, j=mid+1; j<=r;j++) C[i++]=A[j]; intlen2=r-mid; intI=0, j=0, k=l; while(i<len1&&j<len2&&k<=r) {if(B[i]<=c[j]) a[k++]=b[i++]; Else{a[k++]=c[j++]; Ans+=len1-i;//The number of inverse numbers is the number of numbers that are smaller than their own.//at this time b[i]>c[j], then C[j] will give the number of len1-i in reverse order after b[i } } while(I<LEN1) a[k++]=b[i++]; while(J<LEN2) a[k++]=c[j++];}voidMerge_sort (int*a,intLintR) { if(l<r) {intMid= (L+R)/2; Merge_sort (A,l,mid); Merge_sort (A,mid+1, R); Merge (A,l,mid,r); }}intMain () {intN; while(SCANF ("%d", &n) = =1&&N) { for(intI=1; i<=n;i++) scanf ("%d",&A[i]); Ans=0; Merge_sort (A,1, N); printf ("%lld\n", ans); } return 0;}
POJ 2299 Ultra-quicksort (for reverse order number: Discretization + Tree Array or merge sort to find inverse number)