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;
}
主要就是用一个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;
}
相关文章推荐
- mac上用docker,不需要加sudo
- 关于CXF整合Spring时出现jaxws.xsd无法找到的问题
- SPOJ Query on a tree (树链剖分)
- Android常用知识点总汇
- 部分android小知识点记录
- xml读取类
- Android LinearLayout中TextView标签项自动换行的一种解决方案
- hdu 4751 Divide Groups(dfs染色 或 2-sat)
- C语言实现静态链表
- iOS网络开发-AFNetworking请求asp.net WebService
- C#学习笔记(一)
- 详解android中的AsyncTask
- Fragment,ActionBar,ViewPager实现选项卡实现点击切换Fragment
- 第四讲:CCSprite精灵类 -- 创建精灵
- aaaaaaaaaaaaaaaaaaaaaaaa
- poj 2699 The Maximum Number of Strong Kings 【二分 + 竞赛图建模判断是否满流】
- TQ2440挂载NFS
- Java解析XML文件
- 16C554在LINUX上的移植(AT91)
- android webview的使用