UVa 540: Team Queue

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <deque>
#include <cstring>
#include <map>
#include <string>

#define OPEN_DEBUG 1

using namespace std;

/*
map for elements and teams nums.
deque for the queue
*/
int main()
{
    const string ope_stop = "STOP";
    const string ope_enqueue = "ENQUEUE";
    const string ope_dequeue = "DEQUEUE";

    int scene_num = 1;
    int n;
    while(cin >> n && n != 0)
    {
        if(n < 1 || n > 1000)
        {
            continue;
        }   

        int *team_end = new int[n];

        memset(team_end, 0, n*sizeof(int));

        deque<int> t_q;
        map<int, int> map_q;
        for(int i = 0; i < n; i++)
        {
            int num_elsements;
            cin >> num_elsements;
            for(int j = 0; j < num_elsements; j++)
            {
                int element;
                cin >> element; 
                map_q[element] = i;
            }
        }   

        #if(OPEN_DEBUG == 1)
            for (map<int, int>::iterator it = map_q.begin(); it != map_q.end(); it++)
            {
                cout << "1st " << it->first << "  2nd " << it->second << " " << endl;
            }   
            cout << endl;
        #endif  

        string ope;
        cout << "Scenario #" << scene_num << endl;
        while(cin >> ope && ope != ope_stop)
        {
            if(ope_enqueue == ope)
            {
                int num_ele;
                cin >> num_ele;
                int no_team = map_q[num_ele]; 
                int end_pos_team = team_end[no_team];

                #if(OPEN_DEBUG == 1)
                    cout << "Team No." << no_team << " " << end_pos_team << endl;
                    for(int m = 0; m < n; m++)
                    {
                        cout << &team_end[m] << " ";
                    }   
                    cout << endl;
                #endif

                if(!end_pos_team)
                {
                    #if(OPEN_DEBUG == 1)
                        cout << num_ele << " "  << no_team << endl;
                    #endif
                    t_q.push_back(num_ele);
                    int pos = t_q.size();
                    team_end[no_team] = pos;
                }
                else
                {   
                    deque<int>::iterator it = t_q.begin() + end_pos_team;
                    t_q.insert(it, num_ele);
                    for(int j = 0; j < n; j++)
                    {
                        if( team_end[j] >= end_pos_team)
                        {
                            team_end[j]++;
                        }   
                    }                   
                }   

                #if(OPEN_DEBUG == 1)
                    for(deque<int>::iterator it = t_q.begin(); it != t_q.end(); it++)   
                    {
                        cout << *it << " "; 
                    }   
                    cout << endl;
                #endif  
            }   

            if(ope_dequeue == ope)
            {
                if(!t_q.empty())
                {   
                    int tmp_front = t_q.front();
                    cout << tmp_front << endl;
                    t_q.pop_front();
                    // map_q.erase(tmp_front);

                    for(int j = 0; j < n; j++)
                    {
                        if(team_end[j])
                        {
                            team_end[j]--;
                        }   
                    }   
                }   
                #if(OPEN_DEBUG == 1)
                    for(deque<int>::iterator it = t_q.begin(); it != t_q.end(); it++)   
                    {
                        cout << *it << " "; 
                    }   
                    cout << endl;
                #endif  
            }   
        }   

        cout << endl;
        scene_num++;

        delete []team_end;
    }   

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值