/*************************************************************************
> File Name: 540.cpp
> Author: gwq
> Mail: [email protected]
> Created Time: 2015年01月21日 星期三 15時48分29秒
************************************************************************/
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;
const double esp = 1e-5;
#define N 1010
int main(int argc, char *argv[])
{
int n;
int c = 0;
while (scanf("%d", &n) != EOF) {
if (n == 0) {
break;
}
// 記錄所有人的團隊編號
map<int, int> team;
for (int i = 0; i < n; ++i) {
int x, y;
scanf("%d", &x);
while (x--) {
scanf("%d", &y);
team[y] = i;
}
}
queue<int> qteam; // 團體隊列
queue<int> q[N]; // q[i]是團隊i成員的隊列
printf("Scenario #%d\n", ++c);
while (1) {
char cmd[10];
scanf("%s", cmd);
if (cmd[0] == 'S') {
break;
} else if (cmd[0] == 'D') {
int t = qteam.front();
printf("%d\n", q[t].front());
q[t].pop();
if (q[t].empty()) {
qteam.pop(); // 團隊t全體隊員出隊
}
} else {
int x;
scanf("%d", &x);
int t = team[x];
if (q[t].empty()) {
qteam.push(t); // 團隊t進入隊列
}
q[t].push(x);
}
}
printf("\n");
}
return 0;
}
uvaoj 540 Team Queue 隊列模擬
uvaoj 540 Team Queue 隊列模擬
t個團隊的人在排隊,每新來一個人時,如果它有隊友在排隊,那麼這個人就排在最後一個隊友的後邊,如果沒有任何隊友排隊,那麼這個人排到長隊的隊尾,我們可以將這個隊列看做兩級,一級是團隊隊列,一個是團隊自身的隊列。模擬一下就行了。
代碼如下:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.