poj2299--歸併排序求逆序數,poj2299--逆序

來源:互聯網
上載者:User

poj2299--歸併排序求逆序數,poj2299--逆序

/** \brief poj2299 * * \param date 2014/8/5 * \param state AC * \return memory 4640K time 3250ms * */#include <iostream>#include <fstream>#include <cstdio>#include <cstring>using namespace std;const int MAXN=500000;int Arr[MAXN];int T[MAXN];//__int64 t;__int64 t;void merge_sort(int* A,int x,int y,int* T){    if(y-x>1)    {        int m=x+(y-x)/2;//partition        int p=x,q=m,i=x;        merge_sort(A,x,m,T);        merge_sort(A,m,y,T);        while(p<m || q<y)        {            if(q>=y || (p<m && A[p]<=A[q]))                T[i++]=A[p++];//從左半數組複製到臨時空間            else            {                T[i++]=A[q++];//從右半數組……                t+=m-p;//統計逆序數對            }        }        for(int i=x;i<y;i++)            A[i]=T[i];//從輔助空間複製回A數組    }}int main(){    //cout << "Hello world!" << endl;    //freopen("input.txt","r",stdin);    int n=0;    while(scanf("%d",&n))    {        if(n==0)break;        memset(Arr,0,sizeof(Arr));        memset(T,0,sizeof(T));        for(int i=0;i<n;i++)            cin>>Arr[i];        merge_sort(Arr,0,n,T);        printf("%I64d",t);        t=0;        cout<<endl;    }    return 0;}


用歸併排序求序列的逆序數



poj2299歸併(合并)排序逆序數,改錯為何sum總是0; 幫忙加油,快到省賽了

這一個賦值錯了,改成
for(int j=0; j<lr; j++)
right[j]=s[j+ls];
 
高分 一道C語言編程題 解答

/*
用歸併排序求逆序數,時間複雜度 nlg(2n)
*/

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
//數組的最大容量,可以修改為 100000
const int MAX_N=100000;

//參生隨機數的範圍
const int RANGE=100000000;

/*
函數名稱:Random
函數功能:產生隨機資料
輸入:要儲存隨機數的數組 a ,要產生的隨機數個數 n
輸出:無
*/
void Random(int a[],int n)
{
int i;
rand( time(NULL) );
for( i = 0; i < n; i++ )
a[i] = rand()%RANGE;
}

/*
函數名稱:MergeSort
函數功能:歸併排序並求逆序數個數
輸入:要排序的數組 a , 以及要排序的區間 [begin,end]
如果a的元素個數為 n,則排序區間 [begin=0, end=n-1]
輸出 a[begin] 到 a[end] 間的逆序數個數
(資料可能超過了 int 的範圍,採用了更大範圍的 __int64)
*/
__int64 MergeSort(int a[], int begin, int end)
{
if(begin == end) return 0;
int i, j, k, mid;
int temp[MAX_N];
mid = (begin+end)/2;
__int64 ret = MergeSort(a, begin, mid);
ret += MergeSort(a, mid+1, end);
i = begin;
j = mid+1;
for(k=begin; k<=end; k++)
{
if(i<=mid&&a[i]<=a[j] || j>end)
temp[k] = a[i++];
else
{
temp[k] = a[j++];
ret += mid-i+1;
}
}
memcpy(a+begin, temp+begin, (end-begin+1)*sizeof(int));
return ret;
}

/*
函數名稱 Output
函數功能:列印資料
輸入: 數組 a ,要輸出的資料個數 n
輸出: 無
*/
void Output (int a[],int n)
{
int i;
for(i=0;i<n-1;i++)
printf("%d,",a[i]);
printf("%d\n",a[i]);
}

int main()
{
int a[MAX_N];
//產生隨機資料
Random(a,MAX_N);

//輸出隨機資料到檔案
//Output(a,MAX_N);

//對隨機資料歸併排序並求逆序數個數
printf("逆序數個數:%I64d\n&q......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.