HDU1873 看病要排队 优先队列法+朴素做法

题目

在这里插入图片描述
在这里插入图片描述

AC代码

优先队列1

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
	int num,id;
	bool operator<(const node &b) const
	{
		if(num==b.num)	return id>b.id;
		return num<b.num;
	}
}t1,t2; 
int main()
{
	int n,x,y,h;
	string s;
	while(cin>>n)
	{
		priority_queue<node> q[4];
		
		h=1;
		while(n--)
		{
			cin>>s;
			if(s=="IN")
			{
				cin>>x>>y;
				t1.num=y;
				t1.id=h;
				h++;
				
				q[x].push(t1);
			}
			else if(s=="OUT")
			{
				cin>>x;
				
				if(q[x].size()==0) 
					printf("EMPTY\n");
				else
				{
					t2=q[x].top();
					q[x].pop();
					
					cout<<t2.id<<endl;
				}
				
			}
		}
			
	}
	return 0;
}

优先队列2

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
	int num,id;
	friend bool operator<(node n1,node n2)
	{
		if(n1.num==n2.num) return n1.id>n2.id;
		return n1.num<n2.num; 
	}
}t1,t2; 
int main()
{
	int n,x,y,h;
	string s;
	while(cin>>n)
	{
		priority_queue<node> q[4];
		
		h=1;
		while(n--)
		{
			cin>>s;
			if(s=="IN")
			{
				cin>>x>>y;
				t1.num=y;
				t1.id=h;
				h++;
				
				q[x].push(t1);
			}
			else if(s=="OUT")
			{
				cin>>x;
				
				if(q[x].size()==0) 
					printf("EMPTY\n");
				else
				{
					t2=q[x].top();
					q[x].pop();
					
					cout<<t2.id<<endl;
				}
				
			}
		}
			
	}
	return 0;
}

结构体模拟

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct sa{
	int num,id;
}a[2010];
struct sb{
	int num,id;
}b[2010];
struct sc{
	int num,id;
}c[2010];
int main()
{
	int n;
	while(cin>>n)
	{
		int t1=0,t2=0,t3=0;
		int hh=1,x,y;
	
		string s;
		
		while(n--)
		{
			cin>>s;
			if(s=="IN")
			{
				cin>>x>>y;
				if(x==1)
				{
					a[t1].id=hh;
					a[t1].num=y;				
					hh++; t1++;
				}
				else if(x==2)
				{
					b[t2].id=hh;
					b[t2].num=y;
					hh++; t2++;
				}
				else if(x==3)
				{
					c[t3].id=hh;
					c[t3].num=y;
					hh++; t3++;
				}
			}
			else if(s=="OUT")
			{
				cin>>x;
				int u,m=-1;
				if(x==1)
				{
					for(int i=0;i<t1;i++)
					{
						if(a[i].num>m)
						{
							m=a[i].num;
							u=i;
						}
					}
					if(m!=-1)
					{
						printf("%d\n",a[u].id);
						a[u].num=-1;						
					}
					else
						printf("EMPTY\n");

				}
				else if(x==2)
				{
					for(int i=0;i<t2;i++)
					{
						if(b[i].num>m)
						{
							m=b[i].num;
							u=i;
						}	
					}
					if(m!=-1)
					{
						printf("%d\n",b[u].id);
						b[u].num=-1;
					}
					else
						printf("EMPTY\n");
				}
				else if(x==3)
				{
					for(int i=0;i<t3;i++)
					{
						if(c[i].num>m)
						{
							m=c[i].num;
							u=i;
						}
					}
					if(m!=-1)
					{
						printf("%d\n",c[u].id);
						c[u].num=-1;
					}
					else
						printf("EMPTY\n");
				}
			}
		}
		
	}
	return 0;
}
posted @ 2021-07-12 17:19  斯文~  阅读(107)  评论(0)    收藏  举报

你好!