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

我写了个同样的C又WA了

Posted by frankvista at 2010-08-01 23:00:48 on Problem 3253
In Reply To:经过修改,终于AC Posted by:frankvista at 2010-08-01 21:23:32
#include <stdio.h>

#define nmax 25000

typedef long long keytype;
typedef struct sheap
{
	int heapsize;
	keytype key[nmax];
}heap;

heap a;

void keep(int i)
{
	int next=i;
	if(i+i<=a.heapsize)
	{
		if(a.key[i+i]<a.key[next])
			next=i+i;
		if(i+i+1<=a.heapsize && a.key[i+i+1]<a.key[next])
			next=i+i+1;
		if(next!=i)
		{
			a.key[i]^=a.key[next]; a.key[next]^=a.key[i]; a.key[i]^=a.key[next];
			keep(next);
		}
	}
}

keytype insert(keytype x)
{
	a.key[++a.heapsize]=x;
	int now;
	for(now=a.heapsize;now>1 && a.key[now]<a.key[now>>1];now>>=1)
	{
		a.key[now]^=a.key[now>>1]; a.key[now>>1]^=a.key[now]; a.key[now]^=a.key[now>>1];
	}
	return x;
}

keytype delete_min()
{
	keytype ret=a.key[1];
	a.key[1]=a.key[a.heapsize--];
	keep(1);
	return ret;
}

void buildheap()
{
	int i;
	for(i=a.heapsize>>1;i>0;i--)
		keep(i);
}

int main(int argc,char *argv[])
{
	int n,i;
	keytype ans=0;
//	freopen("temp.in","r",stdin); freopen("temp.out","w",stdout);
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a.key[i]);
		ans-=a.key[i];
	}
	a.heapsize = n; buildheap();
	while(1)
	{
		if(a.heapsize==1)
		{
			ans+=a.key[1]; break;
		}
		ans+=insert(delete_min()+delete_min());
	}
	printf("%d\n",ans);
//	fclose(stdin); fclose(stdout);
	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