Ultra-quicksort
Time Limit: 7000MS |
|
Memory Limit: 65536K |
Total Submissions: 45960 |
|
Accepted: 16702 |
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
Source
Waterloo Local 2005.02.05
Data is larger, setting a POS equivalent to discrete
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map > #define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) #define EPS 1e-8typedef __ Int64 ll; #define FRE (i,a,b) for (i = A; I <b; i++) #define FREE (i,b,a) for (i = b; I >= a;i--) #define MEM (T, v) MEMS ET ((t), V, sizeof (t)) #define SSF (n) scanf ("%s", N) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ( "%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF Printf#define Bug PF ("hi\n") using namespace std, #define INF 0x3f3f3f3f#define n 500005int a[n],c[n];int n;struct stud{int pos,x; BOOL operator < (CONST stud &B) Const {return x<b.x; }}f[n];int lowbit (int x) {return x& (-X);} void update (int x) {while (x<=n) {c[x]++;x+=lowbit (x);}} int sum (int x) { int s=0; while (x) {s+=c[x]; X-=lowbit (x); } return s;} int main () {int i,j;while (SF (n), N) {for (i=1;i<=n;i++) {SF (f[i].x); f[i].pos=i;} Sort (f+1,f+n+1); ll Ans=0; MEM (c,0); for (i=1;i<=n;i++) {ans+=sum (n)-sum (f[i].pos-1); update (F[I].POS);} PF ("%i64d\n", ans);} return 0;}
POJ 2299 ultra-quicksort (Inverse number tree array)