C - Ultra-QuickSort(7.2.2)

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

題目大意:

給出長度爲n的序列,每次只能交換相鄰的兩個元素,問至少要交換幾次才使得該序列爲遞增序列。

 

解題思路:

一看就是冒泡,交換一次記錄一次就可以了

但是n的範圍達到50W,冒泡O(n^2)的複雜度鐵定超時(即使有7000ms,其實這是一個陷阱)

直接用快排又不符合題目的要求(相鄰元素交換),快排是建立在二分的基礎上的,操作次數肯定比在所要求的規則下的交換次數要更少

 

那麼該怎麼處理?

 

其實這題題目已經給出提示了:Ultra-QuickSort

特殊的快排,能和快排Quicksort相媲美的就是歸併排序Mergesort了,O(nlogn)

 

但是用歸併排序並不是爲了求交換次數,而是爲了求序列的 逆序數(學過《線代》的同學應該很熟悉了)

一個亂序序列的 逆序數 = 在只允許相鄰兩個元素交換的條件下,得到有序序列的交換次數

 關於歸併排序具體實現請看這篇文章:

遞歸算法學習---歸併排序

例如例子的

9 1 0 5 4

由於要把它排列爲上升序列,上升序列的有序就是  後面的元素比前面的元素大

而對於序列9 1 0 5 4

9後面卻有4個比9小的元素,因此9的逆序數爲4

1後面只有1個比1小的元素0,因此1的逆序數爲1

0後面不存在比他小的元素,因此0的逆序數爲0

5後面存在1個比他小的元素4, 因此5的逆序數爲1

4是序列的最後元素,逆序數爲0

 

因此序列9 1 0 5 4的逆序數 t=4+1+0+1+0 = 6  ,恰恰就是冒泡的交換次數

 

PS:注意保存逆序數的變量t,必須要用__int64定義,int和long都是無法保存的。。。。會導致WA。 以前的long long 在現在的VC編譯器已經無法編譯了。

注意__int64類型的輸出必須使用指定的c格式輸出,printf(“%I64d”,t);

cout是無法輸出__int64類型的

 

序列數組s[]用int就足夠了,每個元素都是小於10E而已



#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int inf=1000000000;  //10E

__int64  cnt;
/*歸併排序

左邊小左邊,左邊++;右邊小取右邊,右邊++*/

template<typename T>

void merge(T array[], int low, int mid, int high)

{

    int k;

    int len_L = mid - low + 1;

    int len_R = high - mid;

    T * left = new T[ len_L + 2 ];

    T * right = new T[ len_R + 2 ];

    int i,j;

    for( i = 1; i <= len_L; i++ )  // 將mid左側的數據拷貝到left數組中
        left[i] = array[low + i - 1];
    left[len_L + 1] = inf;   //設置無窮上界,避免比較大小時越界

    for( j = 1; j <= len_R ; j++ )  //將mid右側的數據拷貝到right數組中
        right[j] = array[mid + j];
    right[len_R + 1] = inf;   //設置無窮上界,避免比較大小時越界

    i=j=1;

    for( k = low; k <= high; )
        if( left[i] <= right[j] )
            array[k++] = left[i++];
        else
        {
            array[k++] = right[j++];
            cnt += len_L - i + 1;    //計算逆序數
        }

    delete left;
    delete right;

}

template<typename T>

void merge_sort(T array[], unsigned int first, unsigned int last)

{

     int mid = 0;

     if(first<last)

     {

         mid = ( first + last ) / 2;


         //mid = (first & last) + ((first ^ last) >> 1);

         merge_sort(array, first, mid);

         merge_sort(array, mid+1,last);

         merge(array,first,mid,last);

     }

}

int main()
{
    int N, i;
    while( cin >> N && N )
    {
        int *a = new int[N + 1];
        for( i = 1;i <= N; i++ )
            cin >> a[i];
        cnt = 0;
        merge_sort( a, 1, N );
        //for( i = 0; i < N; i++ )
          //  cout<< a[i] << ' ';
        cout << cnt << endl;
        delete a;
    }

    return 0;
}



發佈了61 篇原創文章 · 獲贊 164 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章