Ultra-quicksort (poj 2299 merge Sort | | Tree-like array for reverse order)

Source: Internet
Author: User
Tags acos cmath

Language:DefaultUltra-quicksort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 45751 Accepted: 16615

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

Test instructions: To find the number of n in reverse order.

Code:

Merge sort:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 501005#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO  N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ((t), V, si Zeof (t)) #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 DBG pf ("hi\n") typedef long Long ll;using Nam Espace std;ll a[maxn];ll b[maxn];ll n,ans;void MergE (ll a[],ll l,ll mid,ll r) {int i,j,k=l;//int *b=new int[r+1];    Do not apply dynamically, will time out the FRE (i,l,r) b[i]=a[i];    i=l;j=mid+1;        while (i<=mid&&j<=r) {if (B[i]<=b[j]) a[k++]=b[i++];            else {a[k++]=b[j++];        ans+= (mid-i+1);    }} while (I<=mid) a[k++]=b[i++]; while (j<=r) a[k++]=b[j++];//delete []b;}    void Merge_sort (ll a[],ll l,ll R) {if (l>=r) return;    ll mid= (l+r) >>1;    Merge_sort (A,l,mid);    Merge_sort (A,MID+1,R); Merge (a,l,mid,r);}    int main () {int i,j;        while (scanf ("%lld", &n), n) {ans=0;        FRE (i,1,n) scanf ("%lld", &a[i]);        Merge_sort (A,1,n);    PF ("%lld\n", ans); } return 0;}


Tree-like array:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 501005#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO  N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ((t), V, si Zeof (t)) #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 DBG pf ("hi\n") typedef __int64 ll;using Names    Pace std;struct node{int val; int POS;} node[Maxn];int n;ll ans;int bit[maxn];int cmp (Node A,node b) {return a.val>b.val;}    ll sum (int i) {ll s=0;        while (i>0) {s+=bit[i];    i-=i&-i; } return s;}        void Add (int i,int x) {while (i<=n) {bit[i]+=x;    i+=i&-i;    }}int Main () {int i,j;        while (scanf ("%d", &n), N) {FRE (i,0,n+10) bit[i]=0;            FRE (i,1,n) {scanf ("%d", &node[i].val);            Node[i].val;        Node[i].pos=i;        } sort (node+1,node+n+1,cmp);        ans=0;            FRE (i,1,n) {ans+=sum (node[i].pos-1);        Add (node[i].pos,1);    } PF ("%i64d\n", ans); } return 0;}



Ultra-quicksort (poj 2299 merge Sort | | Tree-like array for reverse order)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.