Team Queue(佇列)

ruoye123456發表於2024-09-13
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<string> VS;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
vector<int> vx;
inline int mp(int x) {return upper_bound(vx.begin(),vx.end(),x)-vx.begin();}
inline int log_2(int x) {return 31-__builtin_clz(x);}
inline int popcount(int x) {return __builtin_popcount(x);}
inline int lowbit(int x) {return x&-x;}
const int N = 1e6+10;
int f[N];
bool st[1010];
void solve()
{
	int t;
	int cnt = 0;
	while(cin>>t,t)
	{
		cout<<"Scenario #"<<++cnt<<'\n';
		queue<int> q[t+1];
		
		//用q[0]表示總鏈
		for(int i=1;i<=t;++i)
		{
			st[i] = 0;
			int n;
			cin>>n;
			for(int j=1;j<=n;++j)
			{
				int x;
				cin>>x;
				f[x] = i;
			}
		}
		//本題的核心在於可以多次入隊出隊
		string s;
		while(cin>>s,s!="STOP")
		{
			if(s == "ENQUEUE") 
			{
				int x;
				cin>>x;
				if(!st[f[x]]) 
				{
					q[0].push(f[x]);
					q[f[x]].push(x);
					st[f[x]] = 1;
				}
				else
				{
					q[f[x]].push(x);
				}
			}
			else if(s == "DEQUEUE")
			{	
				//注意需要的p是根節點
				int p = q[0].front();
				cout<<q[p].front()<<'\n';
				q[p].pop();
				if(q[p].empty()) {q[0].pop();st[p] = 0;}
			}
		}
		cout<<'\n';
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T = 1;
	//cin>>T;
	while(T--)
	{
		solve();
	}
}

相關文章