-
Notifications
You must be signed in to change notification settings - Fork 0
/
1057.cpp
136 lines (136 loc) · 2.13 KB
/
1057.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include<stdio.h>
#include<vector>
#include<stack>
#include<string.h>
#include<algorithm>
using namespace std;
//#define DEBUG
int stack_top;
int bucket[100001];
int medP;
int medRank;
int findPre(int x)
{
for(int i=x-1;i>=0;i--)
if(bucket[i]>0)
return i;
return -1;
}
int findNext(int x)
{
for(int i=x+1;i<=100001;i++)
if(bucket[i]>0)
return i;
return -1;
}
void adjustMed()
{
#ifdef DEBUG
printf("begin to adjust, size=%d, medP=%d, medRank=%d\n",stack_top,medP,medRank);
#endif
int r;
if(stack_top%2 == 0)
r=stack_top/2;
else
r=(stack_top+1)/2;
#ifdef DEBUG
printf("r=%d\n",r);
#endif
if(medRank <= r && r <= medRank+bucket[medP]-1)
;
else if(medRank >r)
{
int newMedP=findPre(medP);
int newMedRank=medRank-bucket[newMedP];
medP=newMedP;
medRank=newMedRank;
}
else
{
int newMedP=findNext(medP);
int newMedRank=medRank+bucket[medP];
medP=newMedP;
medRank=newMedRank;
}
#ifdef DEBUG
printf("end adjust, size=%d, medP=%d, medRank=%d\n",stack_top,medP,medRank);
#endif
}
int main()
{
int n;
scanf("%d",&n);
int i;
int s[100001];
stack_top=0;
for(i=0;i<100001;i++)
bucket[i]=0;
for(i=0;i<n;i++)
{
char command[20];
scanf("%s",command);
#ifdef DEBUG
printf("i=%d, command=%s\n",i,command);
#endif
if(strcmp(command,"Pop")==0)
{
if(stack_top==0)
{
printf("Invalid\n");
}
else
{
int t=s[stack_top];
printf("%d\n",t);
stack_top--;
if(medP == t && bucket[medP] ==1 )
{
int newMedP=findPre(medP);
int newMedRank=medRank-bucket[newMedP];
medP=newMedP;
medRank=newMedRank;
}
else if(medP > t)
{
medRank--;
}
bucket[t]--;
adjustMed();
}
}
else if(strcmp(command,"Push")==0)
{
int t;
scanf("%d",&t);
if(stack_top == 0)
{
stack_top++;
s[stack_top]=t;
bucket[t]++;
medP=t;
medRank=1;
}
else
{
stack_top++;
s[stack_top]=t;
bucket[t]++;
if(t<medP)
medRank++;
adjustMed();
}
}
else if(strcmp(command,"PeekMedian")==0)
{
if(stack_top==0)
printf("Invalid\n");
else
{
printf("%d\n",medP);
}
}
else
printf("Invalid\n");
}
return 0;
}