-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode1105.c
More file actions
69 lines (59 loc) · 951 Bytes
/
code1105.c
File metadata and controls
69 lines (59 loc) · 951 Bytes
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
#include <stdio.h>
#include <string.h>
#define MAXNODE 100010
int tree[MAXNODE] = {0};
int size = 0;
void swap(int n1,int n2)
{
int tmp = tree[n1];
tree[n1] = tree[n2];
tree[n2] = tmp;
}
void add_node(int weight)
{
tree[++size] = weight;
int loc = size;
while((loc > 1) && tree[loc] > tree[loc/2]) {
swap(loc,loc/2);
loc /= 2;
}
}
int big_son(int node)
{
int lnode = 2 * node;
if(lnode + 1 > size)
return lnode;
return tree[lnode] > tree[lnode + 1]?lnode :lnode + 1;
}
int del_root()
{
int root_weight = tree[1];
tree[1] = tree[size--];
int loc = 1;
while((2 * loc <= size)) {
int bs = big_son(loc);
if(tree[loc] < tree[bs]) {
swap(loc,bs);
loc = bs;
continue;
}
break;
}
return root_weight;
}
int main()
{
char c;
int N,weight;
scanf("%d",&N);
while(N--) {
getchar();
scanf("%c",&c);
if(c == 'A') {
scanf("%d",&weight);
add_node(weight);
}
else
printf("%d\n",del_root());
}
}