標籤:des style blog http java color
poj2299
Ultra-QuickSort
Time Limit: 7000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
[Submit] [Status] [Discuss]
DescriptionIn 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. InputThe 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.OutputFor 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
59105431230
Sample Output
60
#include <iostream>#include <string.h>#include <stdio.h>///#include <malloc.h> ///要有標頭檔#include <stdlib.h> ///上面的坑了我 會出現CE using namespace std;long long sum; ///開始用int 不斷的WAvoid Merge(int date[],int l,int m,int r){ int i,j,k; int *pd; ///動態記憶體空間的申請 pd=(int*)malloc((r-l+1)*sizeof(int)); ///malloc傳回值是void 用(int*)強制轉換 開闢(r-l+1)個int空間 i=l,j=m+1,k=0; ///k是一個新的東東 while(i<=m&&j<=r) { if(date[j]>=date[i]) { pd[k++]=date[i++]; } else if(date[j]<date[i]) { pd[k++]=date[j++]; sum+=(m+1-i); } } while(i<=m) pd[k++]=date[i++]; while(j<=r) pd[k++]=date[j++]; for(int i=l,k=0;i<=r;i++,k++) date[i]=pd[k]; free(pd); ///釋放pd所佔的空間}///malloc 動態建立記憶體空間 可以是獨立的指標也可以是數組名。。。///隨機分配空間 不包含初始化內容void MergeSort(int date[],int l,int r){ int m; if(l<r) { m=(r+l)/2; MergeSort(date,l,m); MergeSort(date,m+1,r); Merge(date,l,m,r); }}int main(){ int n; int a[500005]; while(cin>>n) { if(n==0) break; sum=0; for(int i=0;i<n;i++) cin>>a[i]; MergeSort(a,0,n-1); printf("%lld\n",sum); } return 0;}
題意:只可以移動連續的兩個數 是數組一遞增的順序排列 最小的移動次數
階梯思路:連續的兩個進行移動 可以用冒泡吧~~~ 不過好像太慢了 逆序數現代有木有學過 逆序數==本題答案
各種排序的簡單介紹及用處:
冒泡排序(BubbleSort):冒泡排序是最慢的排序演算法。在實際運用中它是效率最低的演算法。 時間複雜度是o(n^2) 一趟又一趟的比較 每次迴圈後會少比較一個
void BubbleSort(int a[],int n){ for(int i=n-1; i>0; i--) { exchange=0; for(int j=0; j<=i-1; j++) { if(a[i]>a[j+1]) { swap(a[i],a[j]); exchange=1; } } if(exchange=0) } return;}
並歸排序(MergeSort): 歸併排序先分解要排序的序列,從1分成2,2分成4,依次分解,當分解到1個一組的時候,就可以排序這些分組,然後依次合并回原來的序列中,
這樣就可以排序所有資料。合并排序比堆排序稍微快一點,但是需要比堆排序多一倍的記憶體空間,因為它需要一個額外的數組
///並歸往往不用來直接考察排序 而是用來求逆序數eg:poj2299
void Merge(int date[],int l,int m,int r){ int i,j,k; int *pd; ///動態記憶體空間的申請 pd=(int*)malloc((r-l+1)*sizeof(int)); ///malloc傳回值是void 用(int*)強制轉換 開闢(r-l+1)個int空間 i=l,j=m+1,k=0; ///k是一個新的東東 while(i<=m&&j<=r) { if(date[j]>=date[i]) { pd[k++]=date[i++]; } else if(date[j]<date[i]) { pd[k++]=date[j++]; sum+=(m+1-i); ///逆序數的個數 } } while(i<=m) pd[k++]=date[i++]; while(j<=r) pd[k++]=date[j++]; for(int i=l,k=0;i<=r;i++,k++) date[i]=pd[k]; free(pd); ///釋放pd所佔的空間}///malloc 動態建立記憶體空間 可以是獨立的指標也可以是數組名。。。///隨機分配空間 不包含初始化內容void MergeSort(int date[],int l,int r){ int m; if(l<r) { m=(r+l)/2; MergeSort(date,l,m); MergeSort(date,m+1,r); Merge(date,l,m,r); }}
快速排序(QuickSort): 快速排序是一個就地排序,分而治之,大規模遞迴的演算法。從本質上來說,它是歸併排序的就地版本。快速排序可以由下面四步組成。
(1) 如果不多於1個資料,直接返回。
(2) 一般選擇序列最左邊的值作為支點資料key值。在第一趟比較大小時key值保持不變 一遍後使大的在key值後面 小的在其前面
(3)將序列分成2部分,一部分都大於支點資料,另外一部分都小於支點資料。
(4)對兩邊利用遞迴排序數列。快速排序比大部分排序演算法都要快。
儘管我們可以在某些特殊的情況下寫出比快速排序快的演算法,但是就通常情況而言沒有比它更快的了。快速排序是遞迴的,對於記憶體非常有限的機器來說,它不是一個好的擇
void QuickSort(int a[],int l,int r) { int i,j; i=l;j=r; a[0]=a[i];///令a[0]==key while(i<j) { while(i<j&&a[j]>a[0]) j--; if(i<j) a[i++]=a[j]; while(i<j%%a[i]<a[0]) i++; if(i<j) a[j--]=a[i]; } a[i]=a[0]; if(l<r) QuickSort(a,i,i-1); QuickSort(a,i+1,r); }
堆排序:堆排序適合於資料量非常大的場合(百萬資料)。堆排序不需要大量的遞迴或者多維的暫存數組。這對於資料量非常巨大的序列是合適的。比如超過數百萬條記錄,因為快速排序,
歸併排序都使用遞迴來設計演算法,在資料量非常大的時候,可能會發生堆疊溢位錯誤。堆排序會將所有的資料建成一個堆,最大的資料在堆頂,然後將堆頂資料和序列的最後一個
資料交換。接下來再次重建堆,交換資料,依次下去,就可以排序所有的資料
請見下回分解~~~~~~