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 15926228182 at 2013-08-19 15:53:30 on Problem 2051
第一次做,竟然1A,**人民发来贺电。还没有用STL的水过

#include <cstdio>
using namespace std;
#define N 3010
int ids[N],time[N][2];//time数组有一个保存原值
void swap(int &a,int &b)
{
	a=a^b;b=a^b;a=a^b;
}
void adjuge(int i,int n)//调整堆 
{
	while(1)
	{
		if(((i<<1)<=n&&(time[i<<1][0]<time[i][0]||(time[i<<1][0]==time[i][0]&&ids[i<<1]<ids[i])))||((i<<1|1)<=n&&(time[i<<1|1][0]<time[i][0]||(time[i<<1|1][0]==time[i][0]&&ids[i<<1|1]<ids[i]))))
		{
			i<<=1;
			if(i+1<=n&&(time[i+1][0]<time[i][0]||(time[i+1][0]==time[i][0]&&ids[i+1]<ids[i]))){
				swap(time[i>>1][0],time[i+1][0]);
				swap(time[i>>1][1],time[i+1][1]);
				swap(ids[i>>1],ids[++i]);
			}
			else{
				swap(time[i>>1][0],time[i][0]);
				swap(time[i>>1][1],time[i][1]);
				swap(ids[i>>1],ids[i]);
			}
		}
		else break;
	}
}
void BuildHeap(int n)//初始化堆
{
	for(int i=n>>1;i>=1;i--)
		adjuge(i,n);
}
void FilterHeap(int n,int k)//输出根,改变根,调整根
{
	BuildHeap(n);
	while(k--)
	{
		time[1][0]+=time[1][1];
		printf("%d\n",ids[1]);
		adjuge(1,n);
	}
}
int main()
{
	char s[10];
	int n=0,k;
	while(scanf("%s",s)&&s[0]!='#'){
		++n;
		scanf("%d %d",&ids[n],&time[n][0]);
		time[n][1]=time[n][0];
	}
	scanf("%d",&k);
	FilterHeap(n,k);
	//BuildHeap(n);
	//for(int i=1;i<=n;i++)
	//	printf("(%d,%d) ",ids[i],time[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