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

不知为啥,同样的代码提交出现了tle。好几次啊,后来啥都没改居然就过了。还391ms,代码如下,有兴趣的可以看看什么原因,难道我的人品出问题啦?

Posted by 1805180411 at 2012-10-27 21:16:26 on Problem 2299
#include<iostream>
#include <stdlib.h>
#include<cstdio>
using namespace std;

#define mmax 500001

int a[mmax];
int t[mmax];
__int64 sum ;

void merge(int s,int mid ,int e)
{
	
	int p = s,q = mid + 1;
	int mm = 0;
	while(p <= mid && q <= e)
	{
		if(a[p] > a[q])
		{
			t[mm++] = a[q++];
			sum += mid - p + 1;
		}
		else
			t[mm++] = a[p++];
	}
	while(p <= mid)
		t[mm++] = a[p++];
	while(q <= e)
		t[mm++] = a[q++];
	for(int i = 0;i < mm;i++)
		a[s+i] = t[i];

}

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()
{
	int i;
	int num;
	while(1)
	{
		scanf("%d",&num);
		if(!num)
			break;
		sum = 0;

		for(i = 0;i < num;i++)
			scanf("%d",&a[i]);

		mergeSort(0,num-1);
		printf("%I64d\n",sum);
	}
	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