-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11279.cpp
More file actions
73 lines (73 loc) · 1.2 KB
/
11279.cpp
File metadata and controls
73 lines (73 loc) · 1.2 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
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int h[100001];
int idx=0;
void push(int a)
{
h[++idx]=a;
for(int i=idx;i>1;i/=2)
{
if(h[i/2]<h[i]){
swap(h[i],h[i/2]);
}
else
{
break;
}
}
}
bool isempty()
{
if(idx==0)
return true;
return false;
}
int pop()
{
int retval=h[1];
h[1]=h[idx];
h[idx--]=0;
for(int i=1;i*2<=idx;)
{
if(h[i]>h[i*2]&&h[i]>h[i*2+1])
break;
else
{
if(h[i*2]>h[i*2+1])
{
swap(h[i],h[i*2]);
i=i*2;
}
else{
swap(h[i],h[i*2+1]);
i=i*2+1;
}
}
}
return retval;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(h,-1,sizeof(h));
int n,x;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x;
if(x==0)
{
if(idx==0)
cout<<"0"<<'\n';
else
cout<<pop()<<'\n';
}
else
{
push(x);
}
}
}