#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[100000], pt = -1,b[100001];
void Push(int n){
a[++pt] = n;
b[n] = 1;
}
void Pop(){
if (pt == -1)
printf("Invalid\n");
else {
b[a[pt]] = 0;
printf("%d\n", a[pt--]);
}
}
void PeekMedian(){
if (pt == -1)
printf("Invalid\n");
else {
int i,sum = 0;
for (i = 0; i <= 10000; i++)
{
if (b[i])
sum++;
if ((pt + 1) % 2)
if (sum == (pt + 2) / 2)
break;
if ((pt + 1) % 2 == 0)
if (sum == (pt + 1) / 2)
break;
}
printf("%d\n", i);
}
}
int main(){
freopen("1.in", "r", stdin);
int n,num;
char cmd[15];
scanf("%d", &n);
while (n--){
scanf("%s", &cmd);
if (!strcmp(cmd,"Push")){
scanf("%d", &num);
Push(num);
}
else if (!strcmp(cmd,"Pop"))
Pop();
else if (!strcmp(cmd,"PeekMedian"))
PeekMedian();
}
return 0;
}
pat 1057 超时
最新推荐文章于 2021-03-12 14:40:07 发布