Team Queue
Team Queue |
Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a team queue, for example.
In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue.
Your task is to write a program that simulates such a team queue.
Input
The input file will contain one or more test cases. Each test case begins with the number of teams t ( ). Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. Elements are integers in the range 0 - 999999. A team may consist of up to 1000 elements.Finally, a list of commands follows. There are three different kinds of commands:
- ENQUEUE x - enter element x into the team queue
- DEQUEUE - process the first element and remove it from the queue
- STOP - end of test case
The input will be terminated by a value of 0 for t.
Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should only take constant time.
Output
For each test case, first print a line saying `` Scenario # k ", where k is the number of the test case. Then, for each DEQUEUE command, print the element which is dequeued on a single line. Print a blank line after each test case, even after the last one.Sample Input
2 3 101 102 103 3 201 202 203 ENQUEUE 101 ENQUEUE 201 ENQUEUE 102 ENQUEUE 202 ENQUEUE 103 ENQUEUE 203 DEQUEUE DEQUEUE DEQUEUE DEQUEUE DEQUEUE DEQUEUE STOP 2 5 259001 259002 259003 259004 259005 6 260001 260002 260003 260004 260005 260006 ENQUEUE 259001 ENQUEUE 260001 ENQUEUE 259002 ENQUEUE 259003 ENQUEUE 259004 ENQUEUE 259005 DEQUEUE DEQUEUE ENQUEUE 260002 ENQUEUE 260003 DEQUEUE DEQUEUE DEQUEUE DEQUEUE STOP 0
Sample Output
Scenario #1 101 102 103 201 202 203 Scenario #2 259001 259002 259003 259004 259005 260001
Miguel A. Revilla
1999-01-11
题目大意:
有个所谓的team排序, 给出t个队伍,以及这些队伍的成员。 然后进行排队。
ENQUEUE x: 把x插入队列中时。如果队列没有该元素的队员,则插入队列的最后面。如果队列中已经有了他的队员,那么插入最后一个队员之后。
DEQUEUE: 把队头的元素删除,并且输出
STOP: 停止
还是TLE,后来借鉴了许多人的想法,发现查找时需要用个迭代器数组把每组存放的位置存起来这样就快了很多。。。不然一个个对比查找肯定TLE,这几天总共提交了不下30次。。。虽然耽搁了近4天,但是通过这道题算是对deque,stack,queue,list,map,iterator更加熟悉了,也不算白费我的时间。。
最后我把我的几种思路的代码都贴出来
第一种(TLE)deque,stack模拟
#include<iostream>
#include<deque>
#include<stack>
#include<string>
#include<cstring>
using namespace std;
int tag[1010];
int flag[1000010];
deque<int> team;
stack<int> que;
int main()
{
int n,t=0;
while(cin>>n&&n)
{
memset(tag,0,sizeof(tag));
memset(flag,0,sizeof(flag));
int i,j,k,s,m;
//cout<<"Scenario #"<<++t<<endl;
for(i=0;i<n;i++)
{
cin>>m;
for(j=0;j<m;j++)
{
cin>>s;
flag[s]=i;
}
}
cout<<"Scenario #"<<++t<<endl;
string str;
while(cin>>str&&str!="STOP")
{
if(str=="ENQUEUE")
{
cin>>k;
if(tag[flag[k]]==0)
{
team.push_back(k);
tag[flag[k]]++;
}
else
{
while(flag[team.back()]!=flag[k])
{
que.push(team.back());
team.pop_back();
}
team.push_back(k);
tag[flag[k]]++;
while(!que.empty())
{
team.push_back(que.top());
que.pop();
}
}
}
else if(str=="DEQUEUE")
{
tag[flag[team.front()]]--;
cout<<team.front()<<endl;
team.pop_front();
}
}
}
return 0;
}
第二种(TLE)链表+map,还是超时。。
#include<algorithm>
#include<iostream>
#include<iterator>
#include<cstring>
#include<string>
#include<map>
#include<list>
using namespace std;
map<int,int> arry;
list<int> team;
int tag[1050];
int main()
{
int t=0,n;
while(cin>>n&&n)
{
memset(tag,0,sizeof(tag));
arry.clear();
team.clear();
int i,j,k,m,s;
for(i=0;i<n;i++)
{
cin>>m;
for(j=0;j<m;j++)
{
cin>>s;
arry.insert(make_pair(s,i));
}
}
cout<<"Scenario #"<<++t<<endl;
string str;
int p,q;
map<int,int>::iterator pos;
while(cin>>str&&str!="STOP")
{
if(str=="ENQUEUE")
{
cin>>k;
pos=arry.find(k);
p=pos->second;
if(tag[p]==0)
{
team.push_back(k);
tag[p]++;
}
else
{
pos=arry.find(team.back());
q=pos->second;
list<int>::iterator iter=team.end();
iter--;
while(1)
{
if(p==q) break;
iter--;
pos=arry.find(*iter);
q=pos->second;
}
iter++;
team.insert(iter,k);
tag[p]++;
}
}
else if(str=="DEQUEUE")
{
pos=arry.find(team.front());
int d=pos->second;
tag[d]--;
cout<<team.front()<<endl;
team.pop_front();
}
}
}
return 0;
}
第三种正确的思路:
如果对链表熟悉,要模拟这个过程并不难。 STL 中的list挺好用的, 是双向循环链表。 end()成员函数返回链接首位的那个迭代其。 题目的一个关键过程是进行查询元素x时属于哪个队伍(可以用二分查找加速, 提高的速度很客观),还可以开一个超大的数组,用坐标映射元素值,数组保存队伍号,这样的话查找元素的队伍只需要O(1)的时间,是最快的。 然后更关键的一步是,每次插入位置的查找,如果每次的插入都要进行查找一次位置,肯定会TLE。可以另外开一个迭代器数组保存某队伍在队列中的最后一个位置的迭代器。
#include<algorithm>
#include<iterator>
#include<cstdio>
#include<cstring>
#include<map>
#include<list>
using namespace std;
map<int,int> arry;
list<int> team;
list<int>::iterator itlast[1010];
char str[40];
int main()
{
int t=0,n;
while(scanf("%d",&n)&&n)
{
if(!team.empty()) team.clear();
if(!arry.empty()) arry.clear();
printf("Scenario #%d\n", ++t);
int i,j,k,m,s;
for(i=0;i<n;i++)
{
scanf("%d",&m);
for(j=0;j<m;j++)
{
scanf("%d",&s);
arry.insert(make_pair(s,i));
}
itlast[i]=team.end();
}
int p;
map<int,int>::iterator pos;
while(scanf("%s",str)&&strcmp(str,"STOP")!=0)
{
if(strcmp(str,"ENQUEUE")==0)
{
scanf("%d",&k);
pos=arry.find(k);
p=pos->second;
if(itlast[p]==team.end())
{
itlast[p]=team.insert(itlast[p],k);
}
else if(itlast[p]!=team.end())
{
++itlast[p];
itlast[p]=team.insert(itlast[p],k);
}
}
else if(strcmp(str,"DEQUEUE")==0)
{
if(team.empty()) break;
printf("%d\n",team.front());
for(i=0;i<n;++i)
{
if(itlast[i]==team.begin())
{
itlast[i]=team.end();
break;
}
}
team.pop_front();
}
}
printf("\n");
}
return 0;
}
这都是血淋淋的教训啊,大量数据不能用cin输入啊,太慢了,太多细节没有注意。。把一些错误代码贴在这希望自己长点记性。。
我都不愿意数自己提交多少次了,给这道水题跪了。never give up!