我是靠谱客的博主 自由烤鸡,最近开发中收集的这篇文章主要介绍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;
}

最后

以上就是自由烤鸡为你收集整理的UVa 540: Team Queue的全部内容,希望文章能够帮你解决UVa 540: Team Queue所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部