题解 | #Team Queue#

Team Queue

https://ac.nowcoder.com/acm/problem/50966

数据结构选用

用一个map来存value->team的映射 用一个双向链表作为主队列,其元素类型为(以队名为特征的队列)这是一个结构。

操作

使用字符串比较的方式处理指令

ENQUEUE:遍历列表寻找队伍,找到则插入,找不到则在尾部插入。

DEQUEUE:找begin(),来弹出内容

STOP:清空已有内容,还原状态

代码

struct tqueue{
    queue<int> que;
    int team;
};

map<int,int> arr;//N to team
list<tqueue> que;

int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // LOCAL   
    int k=1;
    int Nteam;
    while(cin>>Nteam&&Nteam!=0){
        arr.clear();
        que.clear();

        printf("Scenario #%d\n",k++);
        for(int iteam=0;iteam<Nteam;++iteam){
            int teamsize;
            cin>>teamsize;
            int tem;
            for(int i=0;i<teamsize;++i){
                cin>>tem;
                arr[tem]=iteam;
            }
        }

        string order;
        list<tqueue>::iterator it;
        while(cin>>order&&order!="STOP"){
            if(order=="ENQUEUE"){
                int NO;
                cin>>NO;
                int team=arr[NO];
                bool notfound=true;
                it=que.begin();
                while(it!=que.end()){
                    if(it->team==team){
                        it->que.push(NO);
                        notfound=false;
                        break;
                    }
                    ++it;
                }
                if(notfound){
                    tqueue temqueue;
                    temqueue.que.push(NO);
                    temqueue.team=team;
                    que.push_back(temqueue);
                }
            }else if(order=="DEQUEUE"){
                printf("%d\n",que.begin()->que.front());
                que.begin()->que.pop();
                if(que.begin()->que.empty()){
                    que.erase(que.begin());
                }
            }
        }
        putchar('\n');
    }
    
}
全部评论

相关推荐

狸猫换offer:埋点都出来了
点赞 评论 收藏
分享
准备进厂的共享单车:你最好现在就开始投吧 投一些中厂左右的公司 因为快寒假实习了 普遍比较好找一点 年后尤其快暑假的前一两个月竞争最激烈,现在投慢慢练面试经验 如果没过就慢慢沉淀 过了也看自身情况直接去实习呗 (有offer也可以不去啊) 有机会的话最好还是直接把握了,一定要等到年后实习吗 找个好实习寒假过年那几天又不是不能回家过年 难道你寒假有其他打算吗
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务