POJ 2299.Ultra-QuickSort

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2299.Ultra-QuickSort相关的知识,希望对你有一定的参考价值。

Ultra-QuickSort
Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

 

逆序数问题

采用归并排序的方法来将时间复杂度从O(n2)到O(nlogn)

 

要特别注意,由于数据非常大,因此最坏情况下答案是超出int范围的,要使用long long保存

 

AC代码:GitHub

 1 /*
 2 By:OhYee
 3 Github:OhYee
 4 HomePage:http://www.oyohyee.com
 5 Email:oyohyee@oyohyee.com
 6 Blog:http://www.cnblogs.com/ohyee/
 7 
 8 かしこいかわいい?
 9 エリーチカ!
10 要写出来Хорошо的代码哦~
11 */
12 
13 #include <cstdio>
14 #include <algorithm>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <iostream>
19 #include <vector>
20 #include <list>
21 #include <queue>
22 #include <stack>
23 #include <map>
24 using namespace std;
25 
26 //DEBUG MODE
27 #define debug 0
28 
29 //循环
30 #define REP(n) for(int o=0;o<n;o++)
31 
32 const int maxn = 500005;
33 int a[maxn];
34 
35 long long ans;
36 
37 //将已经排好序的a[l]~a[mid] a[mid+1]~a[r]拼合起来
38 void merge(int a[],int l,int mid,int r) {
39     int pos1 = l;//左侧的指针
40     int pos2 = mid + 1;//右侧的指针
41     int *temp = new int[r - l + 1];
42     int pos = 0;//临时数组的指针
43     while(pos1 <= mid || pos2 <= r) {
44         if(pos1 > mid) {
45             temp[pos++] = a[pos2++];
46         }
47         if(pos2 > r) {
48             temp[pos++] = a[pos1++];
49         }
50         if(pos1 <= mid&&pos2 <= r) {
51             if(a[pos1] <= a[pos2]) {
52                 temp[pos++] = a[pos1++];
53             } else {
54                 temp[pos++] = a[pos2++];
55                 ans += mid - pos1 + 1;//交换
56             }
57         }
58     }
59     for(int i = 0;i <= r - l;i++)
60         a[l + i] = temp[i];
61 }
62 
63 //归并排序 对a[l]~a[r]排序
64 void mergesort(int a[],int l,int r) {
65     if(l<r) {
66         int mid = (l + r) / 2;
67         mergesort(a,l,mid);
68         mergesort(a,mid + 1,r);
69         merge(a,l,mid,r);
70     }
71 }
72 
73 bool Do() {
74     int n;
75     if(scanf("%d",&n),n == 0)
76         return false;
77 
78     REP(n)
79         scanf("%d",&a[o]);
80 
81     ans = 0;
82     mergesort(a,0,n - 1);
83     printf("%lld\\n",ans);
84     /*REP(n)
85     printf("%d ",a[o]);
86     printf("\\n");*/
87 
88     return true;
89 }
90 
91 int main() {
92     while(Do());
93     return 0;
94 }

 

以上是关于POJ 2299.Ultra-QuickSort的主要内容,如果未能解决你的问题,请参考以下文章

DP50题(转)

POJ题目分类

计算几何专题

计算几何专题

北大ACM - POJ试题分类

转 POJ分类