uva 540

来源:互联网 发布:淘宝金丝绒太极服 编辑:程序博客网 时间:2024/06/04 18:52

还是比较简单的, 模拟某种队列, 用STL做非常方便

主要就是用一个map来记录每个元素所属的队列, 之后要pop就一个个队列输出就行了

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <string>#include <cstring> #include <vector>#include <set>#include <stack>#include <queue>#include <deque> #include <map>using namespace std;const int MAXN = 1000+1;typedef long long LL;/*uva 540关键: 用什么结构,容器去存储 问题: 不同的队列是否可能有相同的元素 - 否 */int t;map<int,int> belong;void qClear(queue<int> & q){while( !q.empty() ){q.pop();}return ;}int main(){//freopen("input2.txt","r",stdin);int Case = 0;while( scanf("%d",&t) && t!=0 ){int s,tmp;queue<int> ques[MAXN],qSeq;belong.clear();for(int i=1; i<=t; i++){scanf("%d",&s);for(int j=0; j<s; j++){scanf("%d",&tmp);belong.insert(pair<int,int>(tmp,i));}} string cmd;printf("Scenario #%d\n",++Case);while( (cin>>cmd) && cmd!="STOP" ){if( cmd=="ENQUEUE" ){scanf("%d",&s);tmp = belong[s];// 需要将s插到队列标号为tmp的最后一个元素后面 if( ques[tmp].size()==0 ){qSeq.push(tmp);}ques[tmp].push(s);}else{tmp = qSeq.front();if( ques[tmp].size()!=0 ){cout << ques[tmp].front() << endl;ques[tmp].pop();}if( ques[tmp].size()==0 ){qSeq.pop();}}//cout << "q1:" << ques[1].size() << " q2:" << ques[2].size() << endl;}printf("\n");}return 0;}


0 0
原创粉丝点击