杭电1873题 看病要排队

题目链接~~>

这题是接触优先队列的第二题代码有点。。。

开始做时一直wa,最后才明白应该把 priority_queue<zha>q1;

等放到循环里面去,委屈。。。

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct zha
{
    int a1;
    int h1;
    friend bool operator<(const zha &a,const zha &b)
    {
        if(a.a1!=b.a1)
        return a.a1 < b.a1 ;
        else return a.h1 > b.h1;

    }

};
struct zhan
{
    int a2;
    int h2;
     friend bool operator<(const zhan &a,const zhan &b)
    {
        if(a.a2!=b.a2)
        return a.a2 < b.a2 ;
        else return a.h2 > b.h2;

    }


};
struct zhang
{
    int a3;
    int h3;
     friend bool operator<(const zhang &a,const zhang &b)
    {
        if(a.a3!=b.a3)
        return a.a3 < b.a3 ;
        else return a.h3 > b.h3;

    }

};
int main()
{
    char s[10];
    int T,n1,n2;int k;
    while(scanf("%d",&T)!=EOF)
    {
    priority_queue<zha>q1;
    priority_queue<zhan>q2;
    priority_queue<zhang>q3;
    zha current1;
    zhan current2;
    zhang current3;
           k=1;
        while(T--)
      {
          scanf("%s",s);
      if(s[0]=='I')
      {
          scanf("%d%d",&n1,&n2);
          if(n1==1)
            {
                current1.a1=n2;
                current1.h1=k;
                q1.push(current1);
            }
         else if(n1==2)
            {
                current2.a2=n2;
                current2.h2=k;
                q2.push(current2);
            }
         else if(n1==3)
         {

                current3.a3=n2;
                current3.h3=k;
                q3.push(current3);
         }
         k++;
      }
      else {
             scanf("%d",&n1);
             if(n1==1)
             {
                 if(q1.empty())
                 {
                     printf("EMPTY\n");
                 }
                 else {
                       current1=q1.top();
                       printf("%d\n",current1.h1);
                       q1.pop();
                 }
             }
             else if(n1==2)
             {
                 if(q2.empty())
                 {
                     printf("EMPTY\n");
                 }
                 else {
                    current2=q2.top();
                    printf("%d\n",current2.h2);
                    q2.pop();
                 }
             }
             else if(n1==3)
             {
                 if(q3.empty())
                 {
                     printf("EMPTY\n");
                 }
                 else {
                    current3=q3.top();
                    printf("%d\n",current3.h3);
                    q3.pop();
                 }
             }
         }
    }
    }
  return 0;
}

别人代码:

/********************************* 
 *    日期:2013-3-16
 *    作者:SJF0115 
 *    题号: HDU 题目1873: 看病要排队
 *    来源:http://acm.hdu.edu.cn/showproblem.php?pid=1873
 *    结果:AC 
 *    来源:2008浙大研究生复试热身赛(2)——全真模拟
 *    总结: 
**********************************/
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;

struct Patient  
{  
	//值
    int priority;
	//编号
    int key;  
    //重载操作符
    friend bool operator < (Patient p1,Patient p2)  
    {    
        if(p1.priority != p2.priority){
			return p1.priority < p2.priority;
		}
		else{
			return p1.key > p2.key;
		}
    }
};

int main(){
	int i,N,k;
	char Type[4];
	int DoctorID,PatientID;
	Patient patient[2001];
	while(scanf("%d",&N) != EOF){
		//定义三个医生
		priority_queue<Patient> Doctor1;
		priority_queue<Patient> Doctor2;
		priority_queue<Patient> Doctor3;
		k = 1;
		while(N--){
			scanf("%s",Type);
			//诊治
			if(strcmp(Type,"IN") == 0){
				//输入病人和医生
				patient[k].key = k;
				scanf("%d %d",&DoctorID,&patient[k].priority);
				//排队
				if(DoctorID == 1){
					Doctor1.push(patient[k]);
				}
				else if(DoctorID == 2){
					Doctor2.push(patient[k]);
				}
				else{
					Doctor3.push(patient[k]);
				}
				k++;
			}
			//出院
			else if(strcmp(Type,"OUT") == 0){
				//医生DoctorID进行了一次诊治,诊治完毕后,病人出院
				scanf("%d",&DoctorID);
				//医生1
				if(DoctorID == 1){
					if(Doctor1.empty()){
						printf("EMPTY\n");
					}
					else{
						printf("%d\n",Doctor1.top().key);
						//出院
						Doctor1.pop();
					}
				}
				//医生2
				else if(DoctorID == 2){
					if(Doctor2.empty()){
						printf("EMPTY\n");
					}
					else{
						printf("%d\n",Doctor2.top().key);
						//出院
						Doctor2.pop();
					}
				}
				//医生3
				else{
					if(Doctor3.empty()){
						printf("EMPTY\n");
					}
					else{
						printf("%d\n",Doctor3.top().key);
						//出院
						Doctor3.pop();
					}
				}
			}
		}
	}
	return 0;
}


别人代码:

#include <cstdio>
#include <algorithm>
#include <queue>

using namespace std;

struct Node
{
    int num,id;
    friend bool operator < (Node a,Node b)
    {
        if(a.num!=b.num)
          return a.num<b.num;
        else
          return a.id>b.id;
    }
};
int main()
{
    int n,A,B,cnt;
    char str[5];
    Node t;
    while(~scanf("%d",&n))
    {
        cnt=0;
        priority_queue <Node>q[4];
        while(n--)
        {
             scanf("%s",str);
             if(str[0]=='I'){
               scanf("%d%d",&A,&B);
               t.id=++cnt;
               t.num=B;
               q[A].push(t);
             }
             else{
               scanf("%d",&A);
               if(!q[A].empty()){
                 t=q[A].top();
                 q[A].pop();
                 printf("%d\n",t.id);
               }
               else
                 printf("EMPTY\n");
           }
        }
    }
    return 0;
}


 


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值