http://poj.org/problem?id=2259
隊列是一種先進先出的數據結構。它只允許在表的前端(front)進行刪除操作,而在表的后端(rear)進行插入操作。進行插入操作的端稱作隊尾,進行刪除操作的前段稱作隊首。隊列中沒有元素時,稱作空隊列。
題目大意:在組隊隊列中,每個元素(element)屬于一支隊伍。如果一個元素將要進入組隊隊列時,它會先從頭到尾先檢查他的隊友(同屬一支隊伍)是否已經在隊列中,如果找到了,他就會緊隨其后進入隊伍。java多線程隊列。如果沒有找到,則他會從隊列末尾入隊,并成為自己隊伍的第一個元素。出隊操作則跟普通隊列一樣:元素在組隊隊列中按從頭到尾的順序出列。
假設現在有n個隊伍,就開n個隊列,其中q[i]表示第i個隊列;另外再開一個隊列qq記錄team前后的信息。
?
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <set> #include <map> #include <cmath> #include <queue> using namespace std; template <class T> void checkmin(T &t,T x) {if(x < t) t = x;} template <class T> void checkmax(T &t,T x) {if(x > t) t = x;} template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;} template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;} typedef pair <int,int> PII; typedef pair <double,double> PDD; typedef long long ll; #define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++) queue <int> q[1010]; queue <int> qq; char od[22]; int belong[1000100]; int cntq[1010]; int main() {int n , cas = 1;while(~scanf("%d",&n) && n) {printf("Scenario #%d\n" , cas ++);for(int i=0;i<n;i++) while(!q[i].empty()) q[i].pop();while(!qq.empty()) qq.pop();for(int i=0;i<n;i++) {int m;scanf("%d",&m);while(m --) {int a;scanf("%d",&a);belong[a] = i;}cntq[i] = 0;}while(scanf("%s",od) && od[0] != 'S') {if(od[0] == 'E') {int a , b;scanf("%d",&a);b = belong[a];q[b].push(a);if(cntq[b] == 0) qq.push(b);cntq[b] ++;}else {int a = qq.front();cntq[a] --;if(cntq[a] == 0) qq.pop();int b = q[a].front();printf("%d\n",b);q[a].pop();}}/*while(!qq.empty()) {int a = qq.front();qq.pop();int b = q[a].front();q[a].pop();}*/puts("");}return 0; }
?