【Map】Double Queue

Double Queue

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13258   Accepted: 5974

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
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

题目大意:

  有三个操作,1 K P:设置点K的优先级为P;

        2 :输出优先级最高的点,并且删除该点

        3 :输出优先级最低的点,并且删除该点  

        0:退出

  用map处理,插入和查找的复杂度都为log n;

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <queue>
 5 #include <stdio.h>
 6 #include <map>
 7 #include <string.h>
 8 #include <stdlib.h>
 9 using namespace std;
10 int main()
11 {
12     int T,a,P,K;
13     map<int,int>ID;
14     map<int,int>::iterator it;
15     while(scanf("%d",&a)!=EOF){
16         switch(a){
17             case 1:
18                 scanf("%d%d",&P,&K);
19                 ID[K]=P;break;
20             case 2:
21                 if(ID.empty())printf("0\n");
22                 else {
23                     it=ID.end();it--;
24                     printf("%d\n",it->second);
25                     ID.erase(it);
26                 }
27                 break;
28             case 3:
29                 if(ID.empty())printf("0\n");
30                 else {
31                     it=ID.begin();
32                     printf("%d\n",it->second);
33                     ID.erase(it);
34                 }
35                 break;
36             case 0: return 0;
37         }
38     }
39
40     return 0;
41 }

时间: 2024-10-11 15:29:20

【Map】Double Queue的相关文章

【POJ3481】【splay】Double Queue

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 i

HDU 4941 Magical Forest 【离散化】【map】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 题目大意:给你10^5个点,每个点有一个数值,点的xy坐标是0~10^9,点存在于矩阵中.然后给出10^5个操作,1代表交换行,2代表交换列,3代表查询坐标为xy点的数值. 数据量很大........ 所以一直没有思路 后来赛后看了题解是先用离散化然后存在线性map里面. #include <iostream> #include <cstdio> #include <map

C++STL之关联容器【map】【set】

map以键-值対的形式组织,键的作用在于索引,而值表示所存储和读取数据. set仅包含一个键,并且有效的支持某个键是否存在的查询. 他们都是基于标准型类库pair实现,该类型在utility头文件中. 一:关于pair类型的操作 pair<T1,T2> p1; //创建一个空pair类型 pair<T1,T2> p1(v1,v2); //创建并初始化 make_pair(v1,v2) //生成pair对象 <,>,==,!=  //类型之间比较,遵循字典序,先比较fir

【LeetCode】队列 queue(共8题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [346]Moving Average from Data Stream [353]Design Snake Game [363]Max Sum of Rectangle No Larger Than K [582]Kill Process [621]Task Scheduler [622]Design Circular Queue [641]Design Circu

codeforces 501 B Misha and Changing Handles 【map】

题意:给出n个名字变化,问一个名字最后变成了什么名字 先用map顺着做的,后来不对, 发现别人是将变化后的那个名字当成键值来做的,最后输出的时候先输出second,再输出first 写一下样例就好理解了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7

【python】多线程queue导致的死锁问题

写了个多线程的python脚本,结果居然死锁了.调试了一整天才找到原因,是我使用queue的错误导致的. 为了说明问题,下面是一个简化版的代码.注意,这个代码是错的,后面会说原因和解决办法. import Queue import threading queue = Queue.Queue() def test(q): while True: if q.qsize() != 0: d = q.get() print d else: break def main(): global queue n

hdoj1075-What Are You Talking About 【map】

http://acm.hdu.edu.cn/showproblem.php?pid=1075 What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 14876    Accepted Submission(s): 4783 Problem Description Ignatius is

【数论】【二次剩余】【map】hdu6128 Inverse of sum

部分引用自:http://blog.csdn.net/v5zsq/article/details/77255048 所以假设方程 x^2+x+1=0 在模p意义下的解为d,则答案就是满足(ai/aj) mod p = d的数对(i,j)的数量(i<j). 现在把问题转化为解这个模意义下的二次方程. x^2+x+1=0 配方:x^2+x+1/4+3/4=0 (x+1/2)^2+3/4=0 同乘4:(2x+1)^2+3=0 即(2x+1)^2=-3 (mod p) 换句话说,我们必须保证-3+p是p

hdoj-1251-统计难题【map】

统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 23029 Accepted Submission(s): 9640 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).