java多線程隊列,Poj2259 Team Queue 【隊列】

 2023-11-18 阅读 22 评论 0

摘要:http://poj.org/problem?id=2259 隊列是一種先進先出的數據結構。它只允許在表的前端(front)進行刪除操作,而在表的后端(rear)進行插入操作。進行插入操作的端稱作隊尾,進行刪除操作的前段稱作隊首。隊列中沒有元素時,稱作空隊列。 題目大意:

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

?

轉載于:https://www.cnblogs.com/aiiYuu/archive/2013/04/07/3003311.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/175577.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息