POJ 3481 Double Queue

来源:互联网 发布:python画一朵玫瑰花 编辑:程序博客网 时间:2024/05/02 17:49

POJ 3481 Double Queue

(中文版)北大2017大数据研究中心夏令营上机考试 G题 双队列

在POJ上找到的英文版原题链接

描述

系统A用来维护客户。每个客户的id用一个正整数K来表示,当客户进入系统时用P来表示此用户的优先度。这个系统有以下请求

0 系统停止运行 1 K P 优先度为P的客户K进入系统 2 找到优先度最高的客户,然后此客户离开系统 3 找到优先度最低的客户,然后此客户离开系统

输入

每行包括一个请求,最后一行包括一个停止请求(代码0)。对于添加客户请求(代码1),优先度都是唯一的。客户的表示K小于106,优先度P小于107,一个客户可能会被添加多次,每次的优先度可能不同。

输出

对于每个请求2和3,程序必须输出一行。这行包括此请求中找到客户的id。如果系统中没有客户,输出0。

样例输入

21 20 141 30 321 10 993220

样例输出

02030100

思路

本题说是双队列,其实可以用C++的STL中的map来作灵活处理。(其实可以试着用数据结构来处理,准备试一下)。而且英文题目中说的很清楚“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.”,即插入的K和P的值都没有重复的,所以可以用map,不存在重复覆盖的问题。利用了map的性质:第一个关键字有序,这样可以省去排序的麻烦。此题需要scanf()和printf(),若用cin和cout会超时。(找到了用cin和cout而不会超时的办法——在main()函数最开始的时候加上std::ios::sync_with_stdio(false);
std::cin.tie(0);
这两行代码就OK啦,参考链接cin,cout的加速)

源代码

#include<iostream>#include<map>using namespace std;int main(){    map<int,int> myMap;    map<int,int>::iterator iter;//注意map的这种用法    int n,K,P,sum=0;    while(scanf("%d",&n)==1 && n!=0){        if(n==1){            //本题换作cin,cout超时了            //cin>>K>>P;            scanf("%d%d",&K,&P);            myMap[P]=K;            sum++;        }        if(n==2){            if(sum==0){                //cout<<0<<endl;                printf("0\n");            }            else{                iter=myMap.end();                iter--;                //cout<<iter->second<<endl;                printf("%d\n",iter->second);                myMap.erase(iter);                sum--;            }        }        if(n==3){            if(sum==0){                //cout<<0<<endl;                printf("0\n");            }            else{                iter=myMap.begin();                //cout<<iter->second<<endl;                printf("%d\n",iter->second);                myMap.erase(iter);                sum--;            }        }    }    return 0;}
原创粉丝点击