[关闭]
@lunar 2016-07-06T07:32:05.000000Z 字数 2640 阅读 1202

HiHo一下 堆 Week28

HiHo


描述

小Ho有一个糖果盒子,每过一段时间小Ho都会将新买来的糖果放进去,同时他也会不断的从其中挑选出最大的糖果出来吃掉,但是寻找最大的糖果不是一件非常简单的事情,所以小Ho希望能够用计算机来他帮忙计算这个问题!

提示:吃糖果吃多了会变胖的!

输入

每个测试点(输入文件)有且仅有一组测试数据。
在一组测试数据中:
第1行为1个整数N,表示需要处理的事件数目。
接下来的M行,每行描述一个事件,且事件类型由该行的第一个字符表示,如果为'A',表示小Ho将一粒糖果放进了盒子,且接下来为一个整数W,表示这颗糖果的重量;如果为'T',表示小Ho需要知道当前盒子中最重的糖果的重量是多少,在知道这个值之后,小Ho会将这颗糖果从盒子中取出并吃掉。
对于100%的数据,满足1<=N<=10^5, 1<=w<=10^5。
对于100%的数据,满足没有2颗糖果的重量是相同的,最开始的时候小Ho的糖果盒子是空的,且每次小Ho想要取出一颗糖果的时候盒子里一定至少有一颗糖果。

输出

在一组测试数据中:

对于每个类型为'T'的时间,输出1个整数W_MAX,表示在这一时刻,盒子中最重的糖果的重量。

样例

  1. 样例输入
  2. 5
  3. A 77751
  4. A 1329
  5. A 26239
  6. A 80317
  7. T
  8. 样例输出
  9. 80317

思路

简单的堆

代码

  1. #include<iostream>
  2. using namespace std;
  3. #define MAX 100005
  4. int heap[MAX];
  5. int amount = 1;
  6. void insert(int x){
  7. int p = amount;
  8. heap[amount++] = x;
  9. while((heap[p>>1]<x)&&(p>>1>=1)){
  10. heap[p] = heap[p>>1];
  11. p = p>>1;
  12. }
  13. heap[p] = x;
  14. }
  15. void down(int p){
  16. if((p<<1)>amount) return;
  17. if(heap[p<<1]>heap[p<<1|1]){
  18. if(heap[p<<1]<heap[p]) return;
  19. else {
  20. int t = heap[p];
  21. heap[p] = heap[p<<1];
  22. heap[p<<1] = t;
  23. p = p <<1;
  24. if((p<<1)<amount) down(p);
  25. }
  26. }else{
  27. if(heap[p<<1|1]<heap[p]) return;
  28. else {
  29. int t = heap[p];
  30. heap[p] = heap[p<<1|1];
  31. heap[p<<1|1] = t;
  32. p = p <<1|1;
  33. if((p<<1)<amount) down(p);
  34. }
  35. }
  36. }
  37. void pop(){
  38. cout <<heap[1]<<endl;
  39. heap[1] = heap[--amount];
  40. int p = 1;
  41. down(p);
  42. }
  43. int main(){
  44. int n,w;
  45. char a;
  46. cin >> n;
  47. while(n--){
  48. cin >> a;
  49. if(a=='A') {
  50. cin >> w;
  51. insert(w);
  52. }
  53. if(a=='T') pop();
  54. }
  55. return 0;
  56. }
  1. //堆优化的Prim
  2. #include<iostream>
  3. using namespace std;
  4. #define MAXN 100004
  5. #define MAXM 1000005
  6. struct ed{
  7. int to;
  8. int weight;
  9. int next;
  10. };
  11. struct edge{
  12. int x;
  13. int y;
  14. int weight;
  15. };
  16. int header[MAXN];
  17. ed tree[MAXM];
  18. edge heap[MAXM];
  19. bool flag[MAXN];
  20. int edgeAmount=1;
  21. void addEdge(int x,int y,int w){
  22. if(header[x]==0)
  23. tree[edgeAmount].next = -1;
  24. else
  25. tree[edgeAmount].next = header[x];
  26. tree[edgeAmount].to = y;
  27. tree[edgeAmount].weight = w;
  28. header[x] = edgeAmount++;
  29. }
  30. void insert(int x,int y,int weight){
  31. int p = edgeAmount++;
  32. heap[p].x = x;
  33. heap[p].y = y;
  34. heap[p].weight = weight;
  35. edge temp = heap[p];
  36. while((heap[p>>1].weight > temp.weight)&&(p>>1 >=1)){
  37. heap[p] = heap[p>>1];
  38. p = p>>1;
  39. }
  40. heap[p] = temp;
  41. }
  42. void down(int p){
  43. int bc=p;
  44. if(p<<1 > edgeAmount) return ;
  45. if(heap[p<<1].weight < heap[p<<1|1].weight)
  46. bc = p<<1;
  47. else
  48. bc = p<<1|1;
  49. if(heap[bc].weight < heap[p].weight){
  50. edge tt = heap[p];
  51. heap[p] = heap[bc];
  52. heap[bc] = tt;
  53. down(bc);
  54. }
  55. }
  56. int pop(){
  57. int val = heap[1].weight;
  58. heap[1] = heap[--edgeAmount];
  59. down(1);
  60. return val;
  61. }
  62. int main(){
  63. int n,m,x,y,w;
  64. scanf("%d%d",&n,&m);
  65. for(int i =1;i<=n;i++) header[i] = 0;
  66. for(int i =1;i<=n;i++) flag[i] = false;
  67. while(m--){
  68. scanf("%d%d%d",&x,&y,&w);
  69. addEdge(x,y,w);
  70. addEdge(y,x,w);
  71. }
  72. int sum =0;
  73. flag[1] = true;
  74. int p =1;
  75. int s = header[p];
  76. edgeAmount = 1;
  77. for(int i=1;i<n;){
  78. while(tree[s].next!=-1){
  79. if(!flag[tree[s].to]) insert(p,tree[s].to,tree[s].weight);
  80. s = tree[s].next;
  81. }
  82. while(((flag[heap[1].x])xor(flag[heap[1].y]))==0) pop();
  83. if(flag[heap[1].x]) {
  84. flag[heap[1].y] = true;
  85. p = heap[1].y;
  86. }else {
  87. flag[heap[1].x] = true;
  88. p = heap[1].x;
  89. }
  90. s = header[p];
  91. sum +=pop();
  92. i++;
  93. }
  94. cout << sum;
  95. return 0;
  96. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注