湖南农业大学第五次model test

来源:互联网 发布:剪娃娃大神知乎 编辑:程序博客网 时间:2024/04/29 12:21

Double Queue

Description

The new founded Balkan Investment Group Bank (BIGBank) opened a new office in Bucharest, 

equipped with a modern computing environment provided by IBM Romania, and using modern information technologies.

 As usual, each client of the bank is identified by a positive integer and, upon arriving to the bank for some services,

 he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the 

software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk 

with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request: 

The system needs to stop serving 

K P

Add client K to the waiting list with priority P 

Serve the client with the highest priority and drop him or her from the waiting list 

Serve the client with the lowest priority and drop him or her from the waiting list 

Input

Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.  

Each line of the input contains one of the possible requests; only the last line contains the stop

Output

request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.   

For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output. 

Sample Input

1 20 14
1 30 3
2
1 10 99
3
2
3
0

Sample Output

0
20
30
10
0

解题思路: 因为每个优先级都不同,可以自己写一个二叉平衡排序树,也可以直接用 c++中的map;

代码如下:

#include <iostream>#include <cstdio>#include <cstdlib>#include <map>using namespace std;int main(){   //freopen("output.txt","w",stdout);int c,id,prior;map<int,int> s;s.clear();map<int,int>::iterator it;while(1){ scanf("%d",&c); if(c==0)break; switch(c) {   case 1:   scanf("%d%d",&id,&prior);   s[prior]=id;   break;     //2是hightest   case 2:   if(!s.empty())   {    it=s.end();  it--;printf("%d\n",(*it).second);    s.erase(it);   }   else   printf("0\n");   break;    //3是lowest   case 3:   if(!s.empty())   {    it=s.begin(); printf("%d\n",(*it).second);    s.erase(it);   }   else   printf("0\n");   break;  } }  return 0;}

方法二: 用set集合

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <cmath>#include <cstdlib>#include <stack>#include <set>#include <algorithm>using  namespace std;#include <fstream>#define M 102struct client{    int k;    int priority;    //设置默认排序规则,重复的元素不会插入    bool operator <(const client &x )const    {        return priority<x.priority;    }};set<client> s;int main(){    int num;    client c;    s.clear();    set<client>::iterator it;    while(~scanf("%d",&num)&&num)    {        switch(num)        {        case 1:            scanf("%d%d",&c.k,&c.priority);            s.insert(c);            break;        case 2:            if(!s.empty())            {                //注意s.end()没有存放元素                it=s.end();it--;                 printf("%d\n",it->k);                s.erase(it);            }            else                printf("0\n");            break;        case 3:            if(!s.empty())            {                it=s.begin();                 printf("%d\n",it->k);                s.erase(it);            }            else                printf("0\n");            break;        }    }    return 0;}







原创粉丝点击