• hdu 1873(看病要排队)


    题目大意:这道题是中文题,读者可直接去OJ上看题目

    解题思路:题意并不难理解。在我们的现实生活中,假如我们要找某一个医生看病,是不是就要到他的那一条队列上去排队???

    而这个队列又能根据多种情况来排序,这时候,我们可以考虑用以下优先队列。。。。


    代码如下:

    版本一(400MS左右):

    /*
     * 1873_2.cpp
     *
     *  Created on: 2013年8月8日
     *      Author: Administrator
     */
    
    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    struct Node {
    	int pri;
    	int num;
    public:
    	friend bool operator<(const Node& a, const Node& b) {
    		if (a.pri != b.pri) {
    			return a.pri < b.pri;
    		}
    
    		return a.num > b.num;
    	}
    };
    
    int main() {
    
    	int n;
    
    	while (cin >> n) {
    		priority_queue<Node> q[4];
            int b,count = 1;
            string str;
            Node a;
    		for (int i = 0; i < n; ++i) {
    
    
    			cin >> str;
    
    
    
    			if (str == "IN") {
    				cin >> b >> a.pri;
    				a.num = count++;
    
    				q[b].push(a);
    
    			} else {
    				cin >> b;
    				if (!q[b].empty()) {
    					a = q[b].top();
    					q[b].pop();
    					cout<<a.num<<endl;
    				}else{
    					cout<<"EMPTY"<<endl;
    				}
    			}
    		}
    	}
    }
    
    



    版本二:

    /*
     * 1873_1.cpp
     *
     *  Created on: 2013年8月8日
     *      Author: Administrator
     */
    
    #include"stdio.h"
    #include"string.h"
    #include"stdlib.h"
    #include"queue"
    using namespace std;
    int tot;
    struct node {
    	int pri;
    	int num;
    	friend bool operator<(node n1, node n2) {
    		if (n1.pri == n2.pri)
    			return n2.num < n1.num;
    		else
    			return n1.pri < n2.pri;
    	}
    };
    int main() {
    	int n;
    	node now;
    	int a, b;
    	char str[20];
    	while (scanf("%d", &n) != -1) {
    		priority_queue<node> q[4];
    		tot = 0;
    		while (n--) {
    			scanf("%s", str);
    			if (strcmp(str, "OUT") == 0) {
    				scanf("%d", &a);
    				if (q[a].empty())
    					printf("EMPTY
    ");
    				else {
    					now = q[a].top();
    					q[a].pop();
    					printf("%d
    ", now.num);
    				}
    			} else if (strcmp(str, "IN") == 0) {
    				scanf("%d%d", &a, &b);
    				now.num = ++tot;
    				now.pri = b;
    				q[a].push(now);
    			}
    		}
    	}
    	return 0;
    }
    


  • 相关阅读:
    【线程控制:线程休眠】
    【线程调度-优先级】
    【多线程实现方案一:继承Thread 类】
    【多线程概述】
    【使用Mybatis-Generator自动生成Mapper、Model、Mapping相关文件】
    【springmvc集成mybatis框架】
    【UltraISO制作centos7系统安装盘】
    【己有原码, 为何还有反码和补码?】
    【原码, 反码, 补码的基础概念和计算方法】
    【数据类型】
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3246622.html
Copyright © 2020-2023  润新知