Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求大侠指点,,修改算导上的归并排序,,有BUG...

Posted by langjun at 2013-01-19 11:16:49 on Problem 2299
#include<iostream>
#include <stdlib.h>
#include<cstdio>
using namespace std;

#define mmax 500001

int a[mmax];
int t[mmax];
int r[mmax /2 + 1], l[mmax /2 + 1];
__int64 sum ;

void merge(int s,int mid ,int e)
{
    int i, j;
    int n1 = mid - s + 1;
    int n2 = e - mid;
    for(i = 0; i < n1; i++)
        l[i] = a[s + i];
    for(j = 0; j < n2; j++)
        r[j] = a[mid + j + 1];
    l[n1] = 0xffffff;
    r[n2] = 0xffffff;
    i = 0;
    j = 0;
    for(int k = s; k <= e; k++)
    {
        if(l[i] <= r[j])
        {
            a[k] = l[i];
            i++;
        }
        else
        {
            if(l[i] != 0xffffff)
                sum += mid - s + 1;
            a[k] = r[j];
            j++;
        }
    }

}

void mergeSort(int s,int e)
{
	if(s < e){
		int mid = (s+e)/2;
		mergeSort(s,mid);
		mergeSort(mid+1,e);
		merge(s,mid,e);
	}
}

int main()
{
    //freopen("2299.txt","r",stdin);
	int i;
	int num;
	while(cin>>num && num)
	{
		sum = 0;

		for(i = 0;i < num;i++)
			cin>>a[i];

		mergeSort(0,num-1);
		//for(int i = 0; i < num; i++)
                //    cout<<a[i]<<" ";
                //cout<<endl;
                cout<<sum<<endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator