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代码。优先队列16MS,刷不进0MS

Posted by yzcnyun at 2010-10-02 17:22:58 on Problem 3253 and last updated at 2010-10-02 17:24:32
#include<stdio.h>
__int64 sum;
int b[20005];
int h,len;
pt(int i)
{
	return i/2;
}
lt(int i)
{
	return 2*i;
}
rt(int i)
{
	return 2*i+1;
}
mh(int i)
{
	int l,r,lg,t;
	l=lt(i);
	r=rt(i);
	if(l<=h&&b[l]<b[i])
		lg=l;
	else
		lg=i;
	if(r<=h&&b[r]<b[lg])
		lg=r;
	if(lg!=i)
	{
		t=b[i];
		b[i]=b[lg];
		b[lg]=t;
		mh(lg);
	}
}
bmh()
{
	int i;
	h=len;
	for(i=len/2;i>0;i--)
		mh(i);
}
int hem()
{
	int min=b[1];
	b[1]=b[h];
	h--;
	mh(1);
	return min;
}
hink(int i,int key)
{
	int t;
	b[i]=key;
	mh(1);
}		
int main()
{
	int n,i,j,t;
	while(scanf("%d",&n)!=EOF) 
		{
		sum=0;
		for(i=1;i<=n;i++)
			scanf("%d",&b[i]);
		len=n;
		bmh();
		while(h>1)
		{
			t=hem();
			t+=b[1];
			sum+=t;
			hink(1,t);
		}
			
		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