-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path756.cpp
More file actions
113 lines (103 loc) · 1.67 KB
/
756.cpp
File metadata and controls
113 lines (103 loc) · 1.67 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*****************************************
* (This comment block is added by the Judge System)
* Submission ID: 72116
* Submitted at: 2018-11-21 12:53:22
*
* User ID: 539
* Username: 55211931
* Problem ID: 756
* Problem Name: Max-Heap
*/
#include <iostream>
using namespace std;
class MyHeap
{
private:
int size = 0;
int items[100000];
public:
void Insert(int key);
int pop();
void print();
};
void MyHeap::Insert(int key)
{
int temp;
int i = size;
items[i] = key;
while (items[i]>items[(i - 1) / 2] && i != 0)
{
temp = items[i];
items[i] = items[(i - 1) / 2];
items[(i - 1) / 2] = temp;
i = (i - 1) / 2;
}
size++;
}
int MyHeap::pop()
{
int temp, value;
int i = size;
value = items[0];
items[0] = items[i - 1];
int hole = 0;
temp = items[hole];
int child = hole * 2 + 1;
for (; hole * 2 + 1 < size;hole = child)
{
child = hole * 2 + 1;
if (child+1<size &&items[child + 1]>items[child])
child++;
if (items[child] > items[hole])
{
temp=items[hole];
items[hole] = items[child];
items[child] = temp;
}
else
{
size--;
return value;
break;
}
}
size--;
return value;
}
void MyHeap::print()
{
int j = 0;
for (int k = 0;k < size;k++)
{
j += items[k];
}
cout << j << endl;;
}
int main()
{
int times;
while (cin >> times)
{
MyHeap h = MyHeap();
for (int i = 0;i < times;i++)
{
char oper;
cin >> oper;
if (oper == 'a')
{
int num;
cin >> num;
h.Insert(num);
}
else if (oper == 'p')
{
h.pop();
}
else
{
h.print();
}
}
}
return 0;
}