Ultra-quicksort
Time Limit: 7000MS |
|
Memory Limit: 65536K |
Total Submissions: 50517 |
|
Accepted: 18534 |
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
Key: Merge sort note changes to the DT master array, because the data is too large 999999999 to be discretized
Merge:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <vector > #include <algorithm>using namespace std;const int Inf=0x3f3f3f3f;const double Pi=acos ( -1.0); typedef long LONG LL; #define MEM (x, y) memset (x,y,sizeof) #define T_T while (t--) #define F (i,x) for (i=1;i<=x;i++) #define SI (x) scanf ( "%d", &x) #define SL (x) scanf ("%lld", &x) #define PI (x) printf ("%d", x) #define PL (x) printf ("%lld", x) #define P_ printf ("") const int Maxn=500010;int DT[MAXN],B[MAXN]; LL ans;void mergesort (int l,int mid,int r) {int Ll=l,rr=mid+1,pos=l;while (ll<=mid&&rr<=r) {if (dt[ll]< =DT[RR]) b[pos++]=dt[ll++];else{ans+=rr-pos;b[pos++]=dt[rr++];}} for (int i=ll;i<=mid;i++) b[pos++]=dt[i];for (int i=rr;i<=r;i++) b[pos++]=dt[i];for (int i=l;i<=r;i++) dt[i]=b [i];} void ms (int l,int r) {if (l<r) {int mid= (l+r) >>1;ms (L,mid); MS (MID+1,R); MergeSort (l,mid,r);}} int main () {int n;while (~scanf ("%d", &n), N) {int i,j;ans=0; F (i,n) SI (Dt[i]); MS (1,N);P l (ANS);p UTS ("");} return 0;}
Ultra-quicksort (merge sort + discretized tree array)