登录以参加训练计划
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25105 Accepted: 10594 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集合做法,不能满分):
#include<iostream>
#include<set>
using namespace std;
struct my{
int k;
int p;
}P;
bool operator < (my a,my b ){
return a.p<b.p;
};
int main(){
int n;
set<my>s;
s.clear();
while(cin>>n,n){
if(n==1){
int k,p;
cin>>k>>p;
P.p=p;
P.k=k;
s.insert(P);
}
if(n==2){
if(!s.empty()){
cout<<s.rbegin()->k<<endl;
s.erase(*(s.rbegin()));
}else{
cout<<0<<endl;
}
}
if(n==3){
if(!s.empty()){
cout<<s.begin()->k<<endl;
s.erase(*(s.begin()));
}else{
cout<<0<<endl;
}
}
}
return 0;
}
答案二(双优先队列):
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<string>
#include<cstring>
using namespace std;
struct Node{
int k;//客户名字
int p;//客户优先值
friend bool operator<(const Node& x,const Node& y){//友元符号重载< 建一个最大堆
return x.p<y.p;
}
friend bool operator>(const Node& x,const Node& y){//建一个最小堆
return x.p>y.p;
}
}; //以上用于分别创建结构体类型的最小堆greater:q2 和最大堆 less:q1
priority_queue<Node,vector<Node>,less<Node> >q1;
priority_queue<Node,vector<Node>,greater<Node> >q2;
int p1[1000005];//看这个数是否在队列中
int p2[10000005];//看这个优先级是否在队列中
// p1和p2先清零, 如30 14入队,则p1[30]++ p2[14]++ 以30和14为下标的数组p1和p2自加1,表示同时存在q1和q2
int main(){
int n,k,p;
memset(p1,0,sizeof(p1));
memset(p2,0,sizeof(p2));
Node t;
while(scanf("%d",&n),n>0){
if(n==1){
scanf("%d%d",&k,&p);
t.k=k;t.p=p;
q1.push(t);q2.push(t);//同时入两个堆
p1[k]++;p2[p]++;//标记>表示客户和优先级出现过 ,且累加次数表示出现次数
}
if(n==2){//大根堆处理
if(q1.empty()==true){printf("0\n"); continue; }//如果一处理就是空表,则输出0
//以下取出的堆顶不能直接叫号,得先看在p1 p2中是否标记在堆
t=q1.top();
while(!q1.empty() && p1[t.k]==0 && p2[t.p]==0){ //p1和p2为0表示是之前删掉的信息,则忽略并删除,取下一个top
q1.pop();
t=q1.top();
}
//以上while结束,表示发现正确的top,但有可能已经全部忽略完直至堆为空,所以在判断一次输出0
if(q1.empty()==true){printf("0\n"); continue;}
else{
printf("%d\n",t.k);
p1[t.k]--;p2[t.p]--;
q1.pop();
}
}
if(n==3){//小根堆处理
if(q2.empty()==true){printf("0\n"); continue; }//如果一处理就是空表,则输出0
//以下取出的堆顶不能直接叫号,得先看在p1 p2中是否标记在堆
t=q2.top();
while(!q2.empty()&&p1[t.k]==0&&p2[t.p]==0){ //p1和p2为0表示是之前删掉的信息,则忽略并删除,取下一个top
q2.pop();
t=q2.top();
}
//以上while结束,表示发现正确的top,但有可能已经全部忽略完直至堆为空,所以在判断一次输出0
if(q2.empty()==true){printf("0\n");continue; }
else{
printf("%d\n",t.k);
p1[t.k]--;p2[t.p]--;
q2.pop();
}
}
}
return 0;
}
- 参加人数
- 1
- 创建人