-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.cpp
More file actions
88 lines (81 loc) · 1.21 KB
/
Heap.cpp
File metadata and controls
88 lines (81 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[20000],n;
void up_heap(int n)
{
while (n>1)
{
if (a[n]>a[n/2]){
swap(a[n],a[n/2]);
n=n/2;
}
else
break;
}
}
void push(int k)
{
if (n<15000){
n++;
a[n]=k;
up_heap(n);
}
}
void down_heap()
{
int i=1,c=1;
while (c*2<=n)
{
c=c*2;
if (c+1<=n && a[c]<a[c+1])
c++;
if (a[i]<a[c])
swap(a[i],a[c]);
else
break;
i=c;
}
}
void pop()
{
a[1]=a[n];
n--;
down_heap();
}
void input()
{
char c;
int k;
freopen("input.inp","r",stdin);
while (scanf("%c",&c)!=EOF)
{
if (c=='+'){
scanf("%i\n",&k);
push(k);
}
else{
int l=a[1];
while (n>0 && a[1]==l){
pop();
}
scanf("\n");
}
}
}
int main()
{
input();
int m=0,res[20000];
while (n>0)
{
m++;
res[m]=a[1];
while(a[1]==res[m])
pop();
}
printf("%i\n",m);
for (int i=1;i<=m;i++)
printf("%i\n",res[i]);
return 0;
}