STL初步:栈,队列和优先队列

一时失言乱红尘 2022-11-27 12:15 334阅读 0赞

集合栈计算机(The SetStack Computer)

有一个专门为了集合运算而设计的“集合栈”计算机。该机器有一个初始为空的栈,并且支持以下操作:

  • PUSH:空集“{}”入栈
  • DUP:把当前栈顶元素复制一份后再入栈
  • UNION:出栈两个集合,然后把两者的并集入栈
  • INTERSECT:出栈两个集合,然后把二者的交集入栈
  • ADD:出栈两个集合,然后把先出栈的集合加入到后出栈的集合中,把结果入栈

每次操作后,输出栈顶集合的大小(即元素个数)。例如栈顶元素是A={ {}, { {}} }, 下一个元素是B={ {}, { { {}}} },则:

  • UNION操作将得到{ {}, { {}}, { { {}}} },输出3.
  • INTERSECT操作将得到{ {} },输出1
  • ADD操作将得到{ {}, { { {}}}, { {}, { {}} } },输出3.

样例输入

  1. 6
  2. PUSH
  3. PUSH
  4. UNION
  5. PUSH
  6. PUSH
  7. ADD

样例输出

  1. 0
  2. 0
  3. 0
  4. 0
  5. 0
  6. 1
  7. #include<set>
  8. #include<stack>
  9. #include<iostream>
  10. #include<map>
  11. #include<vector>
  12. #include<algorithm>
  13. using namespace std;
  14. typedef set<int> Set;
  15. map<Set,int> IDcache; //把集合映射为id
  16. vector<Set> SetCache; //根据id取集合
  17. stack<int> s; //题目中的栈
  18. int ID(Set x) //查找给定集合的id。如果找不到,分配一个新id
  19. {
  20. if(!IDcache.count(x))
  21. {
  22. SetCache.push_back(x); //添加新集合
  23. IDcache[x]=SetCache.size()-1;
  24. }
  25. return IDcache[x];
  26. }
  27. int n;
  28. int main()
  29. {
  30. cin>>n;
  31. while(n--)
  32. {
  33. string exec;
  34. cin>>exec;
  35. if(exec[0] == 'P') s.push(ID(Set()));
  36. else if(exec[0] == 'D') s.push(s.top());
  37. else
  38. {
  39. Set x1 = SetCache[s.top()];
  40. s.pop();
  41. Set x2 = SetCache[s.top()];
  42. s.pop();
  43. Set x;
  44. if(exec[0] == 'U') set_union(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));//!!!inserter
  45. if(exec[0] == 'I') set_intersection(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));//!!!inserter
  46. if(exec[0] == 'A'){
  47. x=x2;
  48. x.insert(ID(x1));
  49. }
  50. s.push(ID(x));
  51. }
  52. cout<<SetCache[s.top()].size()<<endl;
  53. }
  54. }

队列是符合“先进先出”原则的“公平队列”。
STL队列定义在头文件<queue>中,可用queue<int>s方式定义,用push()和pop()进行元素的入队和出队操作,front()取队首元素,但不删除。

团体队列(Team Queue)

有t个团队的人正在排一个长队。每次新来一个人时,如果他有队友在排队,那么这个新人会插队到最后一个队友的身后。如果没有任何一个队友排队,则他会排到长队的队尾。
输入每个团队中所有队员的编号,要求支持如下3种指令(前两种指令可以穿插进行)

  • ENQUEUE x:编号为x的人进入长队
  • DEQUEUE: 长队列队首出队
  • STOP: 停止模拟

对于每一个DEQUEUE指令,输出出对人的编号.

  1. #include<cstdio>
  2. #include<queue>
  3. #include<map>
  4. using namespace std;
  5. const int maxt = 1000 + 10;
  6. int main()
  7. {
  8. int t, kase = 0;
  9. while (scanf("%d", &t) == 1 && t)
  10. {
  11. map<int, int>team; //记录所有人的团队编号
  12. for (int i = 0; i<t; i++) //team[x]表示编号为x的人所在的团队编号
  13. {
  14. int n, x;
  15. scanf("%d", &n);
  16. while (n--)
  17. {
  18. scanf("%d", &x);
  19. team[x] = i;
  20. }
  21. }
  22. printf("Scenario #%d\n", ++kase);
  23. queue<int>q, q2[maxt]; //模拟
  24. for(;;)
  25. {
  26. int x;
  27. char cmd[10];
  28. scanf("%s", cmd);
  29. if (cmd[0] == 'S')
  30. {
  31. break;
  32. }
  33. else if (cmd[0] == 'D')
  34. {
  35. int t = q.front();
  36. printf("%d\n", q2[t].front()); //打印首元素
  37. q2[t].pop(); //首元素出队(先进先出)
  38. if (q2[t].empty()) q.pop(); //团队t全体出队列
  39. }
  40. else if (cmd[0] == 'E')
  41. {
  42. scanf("%d", &x);
  43. int t = team[x];
  44. if (q2[t].empty()) //团队t进入队列
  45. {
  46. q.push(t);
  47. }
  48. q2[t].push(x);
  49. }
  50. }
  51. printf("\n");
  52. }
  53. return 0;
  54. }
优先队列

先出队列是队列中优先级最高的元素,STL中用priority_queue<int>pq来声明,出队方法为top()。
自定义类型也可以组成优先队列,但必须为每个元素定义一个优先级。
例如,要实现一个“个位数大的整数优先级反而小”的优先队列,可以定义一个结构体cmp,重载“()”运算符,用priority_queue<int,vector<int>,cmp> pq的方式定义。

  1. struct cmp
  2. {
  3. bool operator() (const int a,const int b)const //a的优先级比b小时返回true
  4. {
  5. return a%10>b%10;
  6. }
  7. };

简便定义方法:

  1. priority_queue<int,vector<int>,great<int> >pq

注:可用push()和pop()进行元素的入队和出队操作,top()取队首元素,但不删除。

丑数

丑数指不能被2,3,5以外其他素数整除的数。把丑数从小到大排列起来,结果如下:
1,2,3,4,5,6,8,9,10,12,15…
求第1500个丑数。

  1. /*最小的丑数是1,而对于任意丑数,2x,3x,5x也都是丑数。可以用一个优先队列保存所有已生成的丑数,每次取出最小的丑数,生成3个新的丑数。 */
  2. #include<iostream>
  3. #include<vector>
  4. #include<queue>
  5. #include<set>
  6. using namespace std;
  7. typedef long long ll;
  8. const int coeff[3]={ 2,3,5};
  9. int main()
  10. {
  11. priority_queue<ll,vector<ll>,greater<ll> > pq;
  12. set<ll> s;
  13. pq.push(1);
  14. s.insert(1);
  15. for(int i=1;;i++)
  16. {
  17. ll x=pq.top();
  18. pq.pop();
  19. if(i==1500)
  20. {
  21. cout<<"The 1500'th ugly number is "<<x<<".\n";
  22. break;
  23. }
  24. for(int j=0;j<3;j++)
  25. {
  26. ll x2=x*coeff[j];
  27. if(!s.count(x2))
  28. {
  29. s.insert(x2);
  30. pq.push(x2);
  31. }
  32. }
  33. }
  34. return 0;
  35. }

发表评论

表情:
评论列表 (有 0 条评论,334人围观)

还没有评论,来说两句吧...

相关阅读

    相关 队列优先队列

    队列 队列是一种特殊的[线性表][Link 1],特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作

    相关 STL容器之优先队列

    队列的定义: 队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部。priority\_queue特别之处在于,允许用户为队列

    相关 STL优先队列

    STL之优先队列 原本以为priority\_queue很简单,才知道原来懂的只是最简单的形式。 头文件:\include<queue> 优先队列,也就是原来我们学过的堆