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

小菜鸟急需高手帮助优化堆结构!!!

Posted by 2008550914 at 2010-03-25 18:11:38 on Problem 1442
#include<iostream>
#include<cstdio>
#define N 30001
using namespace std;

int a[N],b[N],c[N],d[N],count=0,big=1,small=0;

void Small(int s,int n)
{
	int j,rc;
	rc=c[s];
	for(j=2*s;j<=n;j*=2)
	{
		if(j<n&&c[j]>c[j+1]) ++j;
		if(!(rc>c[j]))
			break;
		c[s]=c[j];
		s=j;
	}
	c[s]=rc;
}

void Big(int s,int n)
{
	int j,rc;
	rc=a[s];
	for(j=2*s;j<=n;j*=2)
	{
		if(j<n&&a[j]<a[j+1]) ++j;
		if(!(rc<a[j]))
			break;
		a[s]=a[j];
		s=j;
	}
	a[s]=rc;
}

void AddSmall(int e)
{
	int i;
	c[++small]=e;
	for(i=small/2;0<i;i--)
		Small(i,small);
}

void AdjustSmall()
{
	int i;
	if(small>1)
	{
		for(i=1;i<small;i++)
			c[i]=c[i+1];
		small--;
		for(i=small/2;i>0;i--)
			Small(i,small);
	}
	else
		small=0;
}

void AddBig(int e)
{
	int i;
	a[++big]=e;
	for(i=big/2;i>0;i--)
		Big(i,big);
}

void AdjustBig(int e)
{
	int i;
	a[1]=e;
	for(i=big/2;i>0;i--)
		Big(i,big);
}

int main()
{
	bool start=false;
	int m,n,i,j;
	scanf("%d %d",&m,&n);
	for(i=1;i<=m;i++)
		scanf("%d",&a[i]);
	for(i=1;i<=n;i++)
		scanf("%d",&b[i]);

	if(b[1]>1)
		for(j=2;j<=b[1];j++)
			if(a[1]<=a[j])
				AddSmall(a[j]);
			else
			{
				AddSmall(a[1]);
				AdjustBig(a[j]);
			}		
			d[count++]=a[1];
			if(small>=1)
			{
				AddBig(c[1]);
				AdjustSmall();
				start=true;
			}
			else
				AddBig(a[big+1]);
			for(i=2;i<=n;i++)
			{
				j=b[i-1]+1;
				if(!start)
					j++;
				for(;j<=b[i]&&b[i]!=b[i-1];j++)
					if(a[1]<=a[j])
						AddSmall(a[j]);
					else
					{
						AddSmall(a[1]);
						AdjustBig(a[j]);
					}			
					d[count++]=a[1];
					start=false;
					if(small>=1)
					{
						AddBig(c[1]);
						AdjustSmall();
						start=true;
					}
					else
						AddBig(a[big+1]);
			}
			for(i=0;i<n;i++)
				printf("%d\n",d[i]);

			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