您的位置:首页 > 其它

uva 540

2015-09-17 00:39 288 查看
还是比较简单的, 模拟某种队列, 用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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: