Ultraviolet A 540 Team Queue

Source: Internet
Author: User

Train of Thought: list + map Simulation
Analysis:
1. There are n teams, and the number of each team is m.
2. Three operations are available now. ENQUEUE x is used to insert x into the queue. If x is already in the queue, insert it directly to the last of the queue, otherwise, it is inserted to the last queue. DEQUEUE directly obtains the output of the first element of the queue and deletes the first element of the queue. STOP is stopped.
3. directly use map and list to simulate the problem. Because the question requires the specific location to be inserted, use list to simulate the problem.

Code:


 

#include<map>#include<list>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAXN = 1010;map<int , int>mp;list<int>ls;list<int>:: iterator it[MAXN];int cnt[MAXN];void insert(int x){    if(ls.size() == 0){        ls.push_back(x);        it[mp[x]] = ls.begin();        return;     }       int tmp = mp[x];    if(it[tmp] == ls.end()){        ls.push_back(x);         it[tmp] = ls.end();        it[tmp]--;    }    else{        list<int>:: iterator tmpIt = it[tmp];        ls.insert(++tmpIt , x);        it[tmp]++;    }}int main(){    int n , m , x;    int Case = 1;    char str[20];    while(scanf("%d" , &n) && n){         mp.clear();         ls.clear();         for(int i = 1 ; i < MAXN ; i++)             it[i] = ls.end();         memset(cnt , 0 , sizeof(cnt));         printf("Scenario #%d\n" , Case++);         for(int i = 1 ; i <= n ; i++){             scanf("%d" , &m);             while(m--){                  scanf("%d" , &x);                   mp[x] = i;             }          }         while(scanf("%s" , str) && str[0] != 'S'){             if(str[0] == 'E'){                 scanf("%d" , &x);                 insert(x);                 cnt[mp[x]]++;             }             else{                 int front = ls.front();                 ls.pop_front();                 int tmp = mp[front];                 printf("%d\n" , front);                 cnt[tmp]--;                 if(cnt[tmp] == 0)                     it[tmp] = ls.end();             }         }         puts("");    }    return 0;}

 

Related Article

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.