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

runtime error

Posted by xuezhongfenfei at 2012-11-17 11:26:56 on Problem 3253
我自己写了一个堆排序算法,竟然runtime error,求大神帮忙啊!
#include<iostream>
using namespace std;
void create(int a[],int n,int m)
{
     int i;
	 for(i=m; i>=1; i--)
	 {
		 int min=i;
		 if(a[2*i]<a[i] && 2*i<=n)
			 min=2*i;
		 if(a[2*i+1]<a[min] && 2*i+1<=n)
			 min=2*i+1;
		 if(min!=i)
		 {
			 int temp=a[i];
			 a[i]=a[min];
			 a[min]=temp;
			 create(a,n,min);
		 }
	 }
}
void delet(int a[],int *p)
{
	(*p)--;
	int n=*p,i,m,pre=0,j; 
	m=a[n+1]; 
	for(i=1; i<=n/2;)
	{
		j=2*i;
		int min=j;//选取两个子结点的最小值
		if(a[j+1]<a[min] && j+1<=n)
           min=j+1;
		if(a[min]<m)
		{
            a[i]=a[min];
			i=min;
		}
		else
		{
			a[i]=m;
			pre=1;
			break;
		}
	}
	if(pre==0)
		a[i]=m;
	return ;
}
void add(int a[],int *p,int s)
{
     (*p)++;
	 int n=*p,i,j,pre=0; 
	 if(n==1)
	 {
		 a[n]=s;
		 return ;
	 }
	 for(i=n; i>=2;)
	 {
		 j=i/2;
		 if(a[j]>s)//小的话就向前放
		 {
			 a[i]=a[j];
			 i=j;
		 }
		 else
		 {
			 a[i]=s;
			 pre=1;
			 break;
		 }
	 }
	 if(pre==0)
		 a[j]=s;
	 return ;
}
int main()
{
	int n,a[20005],i;
	cin>>n;
	for(i=1; i<=n; i++)//足够长没有说刚好的
		cin>>a[i];
 	create(a,n,n/2);//创建堆栈 
	int p,q;
	__int64 sum=0;
	if(n==1)
		sum=a[1];
	else
	{
     while(n>1)
	 {
		q=a[1];
		delet(a,&n);  
		p=a[1];
		delet(a,&n);  
		sum+=q+p;
		add(a,&n,p+q);  
	 }
	}
	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