湖南农业大学第五次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 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
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
Sample Output
解题思路: 因为每个优先级都不同,可以自己写一个二叉平衡排序树,也可以直接用 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;}
- 湖南农业大学第五次model test
- 2014湖南农业大学ACM校赛
- 图论-2014—湖南农业大学ACM
- 第五次
- 第五次
- Linux下校园网锐捷认证(以湖南农业大学校园网为例)
- 华中农业大学第五届程序设计大赛网络同步赛
- 华中农业大学第五届程序设计大赛 EOne Stroke [枚举]【思维】
- 华中农业大学第五届程序设计大赛 ICandies [线段树]【数据结构】
- 华中农业大学第五届程序设计大赛 JColor Circle
- 华中农业大学第五届程序设计大赛 KDeadline []【思维】
- linux epoll Model Test
- 第五次培训
- 第五次作业
- 第五次作业
- 第五次作业
- 第五次作业
- 第五次作业
- 连续整数的固定和
- char* 转化为 unsigned char 16进制
- 第四届蓝桥杯初赛 C/C++ A组 3. 振兴中华 二叉树的深度遍历
- RegistryHelper-注册表辅助类
- Netfilter/iptables的一些新进展
- 湖南农业大学第五次model test
- Blob类型转换为byte数组类型
- 带分数 - 蓝桥杯
- 应用程序性能分析利器 —— Visual Studio Profiler
- 个人博客已经搭建好了,欢迎访问。http://www.anwcl.com/
- Apache Mina使用小记
- 一个 职员的担心 的English
- C100-18 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时 共有5个数相加),几个数相加有键盘控制。
- Javascript继承机制的设计思想