poj3481Double Queue之set解法

Double Queue
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15781 Accepted: 6993
Description

The new founded Balkan Investment Group Bank (BIG-Bank) 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 K 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:

0 The system needs to stop serving
1 K P Add client K to the waiting list with priority P
2 Serve the client with the highest priority and drop him or her from the waiting list
3 Serve the client with the lowest priority and drop him or her from the waiting list
Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.

Input

Each line of the input contains one of the possible requests; only the last line contains the stop-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.

Output

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

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

0
20
30
10
0

首先看一下set基本操作


c++ stl容器set成员函数:begin()–返回指向第一个元素的迭代器,包含下限

c++ stl容器set成员函数:clear()–清除所有元素

c++ stl容器set成员函数:count()–返回某个值元素的个数

c++ stl容器set成员函数:empty()–如果集合为空,返回true

c++ stl容器set成员函数:end()–返回指向最后一个元素的迭代器,不包含上限

c++ stl容器set成员函数:equal_range()–返回集合中与给定值相等的上下限的两个迭代器

c++ stl容器set成员函数:erase()–删除集合中的元素,返回包含删除元素的迭代器.如果ite中包含元素a,那么se.erase(a)和se.erase(ite)和se.erase((*ite).a)(*ite是调用接口获取元素,通过迭代器找到元素的值然后删掉元素)都是可以的.

c++ stl容器set成员函数:find()–返回一个指向被查找到元素的迭代器.

c++ stl容器set成员函数:get_allocator()–返回集合的分配器

c++ stl容器set成员函数:insert()–在集合中插入元素

c++ stl容器set成员函数:lower_bound()–返回指向大于(或等于)某值的第一个元素的迭代器

c++ stl容器set成员函数:key_comp()–返回一个用于元素间值比较的函数

c++ stl容器set成员函数:max_size()–返回集合能容纳的元素的最大限值

c++ stl容器set成员函数:rbegin()–返回指向集合中最后一个元素的反向迭代器

c++ stl容器set成员函数:rend()–返回指向集合中第一个元素的反向迭代器

c++ stl容器set成员函数:size()–集合中元素的数目

c++ stl容器set成员函数:swap()–交换两个集合变量

c++ stl容器set成员函数:upper_bound()–返回大于某个值元素的迭代器

c++ stl容器set成员函数:value_comp()–返回一个用于比较元素间的值的函数


/*分析:遇到1存数据,遇到2或3根据优先级取值.
而set底层采用红黑树,可以把数据直接插入存取并且自动排序.对于这种有两个数据相关联的,采用pair比较简单,当然也可以采用自定义结构体.
set的排序的自动排序为升序,如果是pair按照第一个元素排序,因此这里就不用写cmp函数,然后set<int,int,cmp> se这样定义了*/
/*
* Filename:    code.cpp
* Created:     2017-07-22
* Author:        wyl6 
*[mail:17744454343@163.com]
* Desciption:  Desciption
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <bitset>
#include <list>
#include <sstream>
#include <set>
#include <functional>
using namespace std;

#define INT_MAX 1 << 30
#define MAX 100
typedef long long ll;
int n;
set<pair<int,int> > se;//>>中间加个空格,防止错认为'>>'
set<pair<int,int> >::iterator ite;

int main()
{
    while (scanf("%d",&n) != EOF && (n != 0)){
        if (n == 1){
            pair<int,int> pa;
            scanf("%d%d",&pa.second,&pa.first);
            se.insert(pa);
        }else if (n == 2){
            if (se.empty()) printf("0\n");
            else{
                ite = se.end();
                ite--;
                printf("%d\n",(*ite).second);
                se.erase(ite);
            }
        }else if (n == 3){
            if (se.empty()) printf("0\n");
            else{
                ite = se.begin();
                printf("%d\n",(*ite).second);
                se.erase(ite);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值