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;}