UVa 540:team QUEUE Data Structure topic

Source: Internet
Author: User
Tags cas printf sort strcmp

Topic Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_ problem&problem=481

Type of topic: data structure, two fork tree

Sample input:

2
3 102
3 201 203 ENQUEUE The ENQUEUE
201
ENQUEUE 102 ENQUEUE ENQUEUE >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
enque UE 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
102
201
203

Scenario #2
259001
259002 259003
259004
259005
260001

The main effect of the topic:

There's a so-called team sort, giving out T teams and members of these teams. and then line up.

ENQUEUE x: Inserts an X into the queue. If the queue does not have a member of that element, the last face of the queue is inserted. If there is already a member of his team in the queue, then insert the last player.

Dequeue: Remove the elements of the team head and output

Stop: Stopping

Ideas for solving problems:

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45535.htm

If you are familiar with the linked list, it is not difficult to simulate this process. The list in STL is very useful, is a two-way circular chain list. The end () member function returns the iteration of the first link. A key process for a topic is which team to query element x (can be accelerated with a binary lookup, improve the speed is very objective), you can also open a large array, with coordinate mapping element values, the array to save the team number, so that the search elements of the team only need O (1) time, is the fastest. A more critical step, then, is that every time you insert a search, you'll be tle if you have to find it once for each insertion. You can also open an iterator array to hold the iterator of the last position in the queue for a team.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <list> #include &LT;STRING&G  
T  
    
#include <algorithm> using namespace std;  
int T, Commandnum,posteam;  
int team[1005][1005];  
    
Char command[10];  
list<int>m_list;  
List<int>::iterator lastit[1005];  int ele[1000000]; Used to map which team the element belongs to.   
Subscript corresponds to the element value//Find out which troop this number is in.  
    int searchteam (int num) {int i,j; For (i=0 i<t; ++i) {//Two point lookup if (Binary_search (team[i]+1, team[i]+1+team[i][0, num)) r  
    Eturn i; //If not found, return-1.   
In fact, the topic to the element must belong to a certain team, is I want to complicate the return-1;  
    } void Input () {int i,j;  
        For (i=0 i<t; ++i) {scanf ("%d", &team[i][0]);  
            For (j=1 j<=team[i][0]; ++j) {scanf ("%d", &team[i][j]);  
        ELE[TEAM[I][J]] = i;  
        } Lastit[i] = M_list.end ();    To sort, prepare for two points in sort (team[i]+1, team[i]+1+team[i][0]);
    }//queue void Enqueue (int num) {posteam=searchteam (num);    
    Use the following method to get the team faster//posteam = Ele[num]; if (lastit[posteam] = = M_list.end ()) {//inserted in M_list.end (), the effect is the same as push_back lastit[posteam] = M_list.inser   
    T (Lastit[posteam], num);  
        } else{++lastit[posteam];  
           
    Lastit[posteam] = M_list.insert (Lastit[posteam], num);  
    } void Dequeue () {if (M_list.empty ()) return;   
    printf ("%d\n", M_list.front ());  
    List<int>::iterator it = Lastit[m_list.front ()];  
            If the pop-up element is the last of the remaining teams in the queue, modify the for (int i=0; i<t; ++i) {if (Lastit[i]==m_list.begin ()) {  
            Lastit[i] = M_list.end ();  
        Break  
} m_list.pop_front ();  
    } void Solve () {if (!m_list.empty ()) m_list.clear ();  
        while (~SCANF ("%s", command)) {if (!strcmp (command, "STOP")) break; if (!strcmp command, "EnqueUE ")) {scanf ("%d ", &commandnum);  
        Enqueue (Commandnum);  
        else if (!strcmp (command, "Dequeue")) {dequeue ();  
    ()} int main () {freopen ("Input.txt", "R", stdin);  
    int Cas=1;  
        while (~SCANF ("%d", &t) && t) {Input ();  
        printf ("Scenario #%d\n", cas++);  
        Solve ();  
    printf ("\ n");  
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.