540:Team Queue

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37754288/article/details/79968718

Team Queue


本题有两个队列:每个团队有一个队列,而团队整体又形成一个队列,建两个queue即可。

#include<cstdio>
#include<map>
#include<queue>
using namespace std;
const int maxt = 1000 + 5;
int t,n;
char cmd[10];
int main(){
    int kase = 0;
    while(scanf("%d",&t) && t){
        queue<int>q1,q2[maxt];
        map<int,int>group;
        int member;
        for(int i = 0;i < t;i++){
            scanf("%d",&n);
            while(n--){
                scanf("%d",&member);
                group[member] = i;
            }
        }
        printf("Scenario #%d\n",++kase);
        while(scanf("%s",cmd) && cmd[0] != 'S'){
            if(cmd[0] = 'E'){
                scanf("%d",&membet);
                int t = group[x];
                if(q2[t].empty()) q1.push(t);
                q2[t].push(x);
            }
            else{
                int t = q1.front();
                printf("%d\n",q2[t].front());
                q2[t].pop();
                if(q2[t].empty()) q1.pop(); //看没人了才pop
            }
        }
        putchar('\n');
    }
    return 0;
}

Team Queue

05-07

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.nIn 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.nnYour task is to write a program that simulates such a team queue.nnnInputnnThe input will contain one or more test cases. Each test case begins with the number of teams t (1<=t<=1000). 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. nnFinally, a list of commands follows. There are three different kinds of commands:nnENQUEUE x - enter element x into the team queuenDEQUEUE - process the first element and remove it from the queuenSTOP - end of test casenThe input will be terminated by a value of 0 for t. nnnOutputnnFor 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.nnnSample Inputnn2n3 101 102 103n3 201 202 203nENQUEUE 101nENQUEUE 201nENQUEUE 102nENQUEUE 202nENQUEUE 103nENQUEUE 203nDEQUEUEnDEQUEUEnDEQUEUEnDEQUEUEnDEQUEUEnDEQUEUEnSTOPn2n5 259001 259002 259003 259004 259005n6 260001 260002 260003 260004 260005 260006nENQUEUE 259001nENQUEUE 260001nENQUEUE 259002nENQUEUE 259003nENQUEUE 259004nENQUEUE 259005nDEQUEUEnDEQUEUEnENQUEUE 260002nENQUEUE 260003nDEQUEUEnDEQUEUEnDEQUEUEnDEQUEUEnSTOPn0nnnSample OutputnnScenario #1n101n102n103n201n202n203nnScenario #2n259001n259002n259003n259004n259005n260001nn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭