POJ 2299 Ultra-QuickSort 题解

Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 53630   Accepted: 19693

Description

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two 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 how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the 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 not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0

Source

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

——————————————————我是分割线————————————————————————

好题。归并排序求逆序对。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<cstdlib>
 8 #include<iomanip>
 9 #include<cassert>
10 #include<climits>
11 #define maxn 10001
12 #define F(i,j,k) for(int i=j;i<=k;i++)
13 #define FF(i,j,k) for(int i=j;i>=k;i--)
14 #define inf 0x7fffffff
15 #define NN 500004
16 #define NL 1000
17 #define mem(a) memset(a, 0, sizeof(a))
18 using namespace std;
19 int N, A[500010], T[500010];
20 __int64 ans;
21 int read(){
22     int x=0,f=1;char ch=getchar();
23     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
24     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
25     return x*f;
26 }
27 __int64 res;
28 int b[NN];
29 void copy(int a[], int l, int r){
30     int i;
31     for (i = l; i <= r; i++){
32         a[i] = b[i];
33     }
34 }
35 void merge(int a[], int l, int mid, int r){
36     int i = l;
37     int j = mid + 1;
38     int k = l;
39     while(i <= mid && j <= r){
40         if(a[i] < a[j]){
41             b[k++] = a[i];
42             i++;
43         }else{
44             b[k++] = a[j];
45             j++;
46             res += mid - i + 1;
47         }
48     }
49     while(i <= mid){
50         b[k++] = a[i];
51         i++;
52     }
53     while(j <= r){
54         b[k++] = a[j];
55         j++;
56     }
57 }
58 void mergeSort(int a[], int l, int r){
59     if(l < r){
60         int mid = (l + r) >> 1;
61         mergeSort(a, l, mid);
62         mergeSort(a, mid + 1, r);
63         merge(a, l, mid, r);
64         copy(a, l, r);
65     }
66 }
67 int main() {
68     int n, i;
69     int f[NN];
70     while(cin>>n&&n){
71         if(n == 0) break;
72         for (i = 1; i <= n; i++){
73             cin>>f[i];
74         }
75         res = 0;
76         mergeSort(f, 1, n);
77         cout<<res<<endl;
78     }
79     return 0;
80 }
poj 2299

 

posted @ 2016-07-03 09:05  SBSOI  阅读(222)  评论(0编辑  收藏  举报