#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
stack<int>st;
int a[100005];
void update(int p,int x)
{
while(p<=100003)
{
a[p]+=x;
p +=p&-p;
}
}
int query(int p)
{
int ret = 0;
while(p>0)
{
ret+=a[p];
p -= p&-p;
}
return ret;
}
void bs(int x)
{
int l = 0,h = 100002;
while(l<h-1)
{
int mid = (l+h)>>1;
if(query(mid)>=x)h = mid;
else l = mid;
}
printf("%d\n",h);
}
int main()
{
int n,i,j,k,nm;
char s[15];
scanf("%d",&n);
nm = 0;
for(i = 1;i<=n;i++)
{
scanf("%s",s);
if(s[1] == 'o')
{
if(nm == 0)printf("Invalid\n");
else
{
k = st.top();
printf("%d\n",k);
st.pop();
update(k,-1);
nm--;
}
}
else if(s[1] == 'e')
{
if(nm == 0)
{
printf("Invalid\n");
continue;
}
if(nm%2)bs(nm/2+1);
else bs(nm/2);
}
else
{
scanf("%d",&k);
st.push(k);
update(k,1);
nm++;
}
}
return 0;
}
PAT 1057 Stack 树状数组
最新推荐文章于 2023-11-26 17:09:54 发布