优先队列

柔光的暖阳◎ 2022-06-17 05:41 402阅读 0赞

点击打开链接

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <queue>
  6. #include <functional>
  7. using namespace std;
  8. #define mod 1000000007
  9. struct node
  10. {
  11. int x;//两种写法都行
  12. /*bool friend operator<(node a,node b)
  13. {
  14. return a.x%3<b.x%3||a.x%3==b.x%3&&a.x<b.x;
  15. }*/
  16. bool operator<(const node &b)const
  17. {
  18. return x%3<b.x%3||x%3==b.x%3&&x<b.x;
  19. }
  20. };
  21. int main()
  22. {
  23. int n,m;
  24. int num[10000];
  25. long long ans,sum;
  26. node t;
  27. while(~scanf("%d %d",&n,&m))
  28. {
  29. priority_queue<node>q;
  30. sum=0;
  31. for(int i=0; i<n; i++)
  32. {
  33. scanf("%d",&t.x);
  34. q.push(t);
  35. sum+=t.x/3+(t.x%3==0?0:1);
  36. }
  37. ans=sum;
  38. while(m--)
  39. {
  40. if(!q.empty())
  41. {
  42. t=q.top();
  43. q.pop();
  44. sum-=t.x/3+(t.x%3==0?0:1);
  45. t.x-=2;
  46. if(t.x>0)
  47. {
  48. sum+=t.x/3+(t.x%3==0?0:1);
  49. q.push(t);
  50. }
  51. ans=(ans+sum)%mod;
  52. }
  53. }
  54. printf("%lld\n",ans);
  55. }
  56. return 0;
  57. }
  58. /*优先队列的基本使用 2010/7/24 dooder*/
  59. #include<stdio.h>
  60. #include<functional>
  61. #include<queue>
  62. #include<vector>
  63. using namespace std;
  64. //定义结构,使用运算符重载,自定义优先级1
  65. struct cmp1{
  66. bool operator ()(int &a,int &b){
  67. return a>b;//最小值优先
  68. }
  69. };
  70. struct cmp2{
  71. bool operator ()(int &a,int &b){
  72. return a<b;//最大值优先
  73. }
  74. };
  75. //定义结构,使用运算符重载,自定义优先级2
  76. struct number1{
  77. int x;
  78. bool operator < (const number1 &a) const {
  79. return x>a.x;//最小值优先
  80. }
  81. };
  82. struct number2{
  83. int x;
  84. bool operator < (const number2 &a) const {
  85. return x<a.x;//最大值优先
  86. }
  87. };
  88. int a[]={14,10,56,7,83,22,36,91,3,47,72,0};
  89. number1 num1[]={14,10,56,7,83,22,36,91,3,47,72,0};
  90. number2 num2[]={14,10,56,7,83,22,36,91,3,47,72,0};
  91. int main()
  92. { priority_queue<int>que;//采用默认优先级构造队列
  93. priority_queue<int,vector<int>,cmp1>que1;//最小值优先
  94. priority_queue<int,vector<int>,cmp2>que2;//最大值优先
  95. priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
  96. //这是右移运算符,所以这里用空格号隔开
  97. priority_queue<int,vector<int>,less<int> >que4;最大值优先
  98. priority_queue<number1>que5;
  99. priority_queue<number2>que6;
  100. int i;
  101. for(i=0;a[i];i++){
  102. que.push(a[i]);
  103. que1.push(a[i]);
  104. que2.push(a[i]);
  105. que3.push(a[i]);
  106. que4.push(a[i]);
  107. }
  108. for(i=0;num1[i].x;i++)
  109. que5.push(num1[i]);
  110. for(i=0;num2[i].x;i++)
  111. que6.push(num2[i]);
  112. printf("采用默认优先关系:\n(priority_queue<int>que;)\n");
  113. printf("Queue 0:\n");
  114. while(!que.empty()){
  115. printf("%3d",que.top());
  116. que.pop();
  117. }
  118. puts("");
  119. puts("");
  120. printf("采用结构体自定义优先级方式一:\n(priority_queue<int,vector<int>,cmp>que;)\n");
  121. printf("Queue 1:\n");
  122. while(!que1.empty()){
  123. printf("%3d",que1.top());
  124. que1.pop();
  125. }
  126. puts("");
  127. printf("Queue 2:\n");
  128. while(!que2.empty()){
  129. printf("%3d",que2.top());
  130. que2.pop();
  131. }
  132. puts("");
  133. puts("");
  134. printf("采用头文件\"functional\"内定义优先级:\n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)\n");
  135. printf("Queue 3:\n");
  136. while(!que3.empty()){
  137. printf("%3d",que3.top());
  138. que3.pop();
  139. }
  140. puts("");
  141. printf("Queue 4:\n");
  142. while(!que4.empty()){
  143. printf("%3d",que4.top());
  144. que4.pop();
  145. }
  146. puts("");
  147. puts("");
  148. printf("采用结构体自定义优先级方式二:\n(priority_queue<number>que)\n");
  149. printf("Queue 5:\n");
  150. while(!que5.empty()){
  151. printf("%3d",que5.top());
  152. que5.pop();
  153. }
  154. puts("");
  155. printf("Queue 6:\n");
  156. while(!que6.empty()){
  157. printf("%3d",que6.top());
  158. que6.pop();
  159. }
  160. puts("");
  161. return 0;
  162. }
  163. /*
  164. 运行结果 :
  165. 采用默认优先关系:
  166. (priority_queue<int>que;)
  167. Queue 0:
  168. 91 83 72 56 47 36 22 14 10 7 3
  169. 采用结构体自定义优先级方式一:
  170. (priority_queue<int,vector<int>,cmp>que;)
  171. Queue 1:
  172. 3 7 10 14 22 36 47 56 72 83 91
  173. Queue 2:
  174. 91 83 72 56 47 36 22 14 10 7 3
  175. 采用头文件"functional"内定义优先级:
  176. (priority_queue<int,vector<int>,greater<int>/less<int> >que;)
  177. Queue 3:
  178. 3 7 10 14 22 36 47 56 72 83 91
  179. Queue 4:
  180. 91 83 72 56 47 36 22 14 10 7 3
  181. 采用结构体自定义优先级方式二:
  182. (priority_queue<number>que)
  183. Queue 5:
  184. 3 7 10 14 22 36 47 56 72 83 91
  185. Queue 6:
  186. 91 83 72 56 47 36 22 14 10 7 3
  187. */

发表评论

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

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

相关阅读

    相关 队列优先队列

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

    相关 优先队列

    优先队列:顾名思义,首先它是一个队列,但是它强调了“优先”二字,所以,已经不能算是一般意义上的队列了,它的“优先”意指取队首元素时有一定的选择性,即根据元素的属性选择某一项值最

    相关 优先队列

    [为什么80%的码农都做不了架构师?>>> ][80_] ![hot3.png][] 优先队列是不同于先进先出队列的另一种队列。每次从队列中取出的是具有最高优先权的元素。