Subject to the effect
A stack is now given, then given three operations
- The number x onto the stack
- The top of the stack out of the stack
- Asked 栈的 median
Entry
Each test case comprising a
first row is a positive integer
, then there
line command format is as follows
Push key
Pop
PeekMedian
Push key
The key
stack, Pop
the top element from the stack, PeekMedian
find the median of all elements in the stack. key
Is a positive integer not more than
Export
Each Pop
output an instruction stack elements, each PeekMedian
median of all elements in the stack output command. If an element can not stack or the stack output is outputInvalid
Sample input
17
Pop
PeekMedian
Push 3
PeekMedian
Push 2
PeekMedian
Push 1
PeekMedian
Pop
Pop
Push 5
Push 4
PeekMedian
Pop
Pop
Pop
Pop
Sample Output
Invalid
Invalid
3
2
2
1
2
4
4
5
3
Invalid
Resolve
This problem can be used Fenwick tree to solve.
But the tree is stored in the array subscript i is the number of how many before i , and so seek prefix and then compare the size of the stack can be found with a median stack Fenwick tree
but this problem will be with python timeout, c ++ can be AC
timeout python:
# -*- coding: utf-8 -*-
# @Time : 2019/6/13 11:10
# @Author : ValarMorghulis
# @File : 1057.py
s = list()
c = [0 for i in range(100861)]
def lowBit(x):
return x & (-x)
def update(x, v):
i = x
while i < 100861:
c[i] += v
i += lowBit(i)
def getSum(x):
sum, i = 0, x
while i >= 1:
sum += c[i]
i -= lowBit(i)
return sum
def find():
left, right, k = 1, 100861, (len(s) + 1) // 2
while left < right:
mid = (left + right) // 2
if getSum(mid) >= k:
right = mid
else:
left = mid + 1
print(left)
def solve():
global c, s
n = int(input())
for i in range(n):
command = input().split()
if command[0][1] == 'u':
s.append(int(command[1]))
update(int(command[1]), 1)
elif command[0][1] == 'o':
if len(s) == 0:
print("Invalid")
else:
update(s[-1], -1)
print(s[-1])
s.pop()
elif command[0][1] == 'e':
if len(s) == 0:
print("Invalid")
else:
find()
if __name__ == "__main__":
solve()
AC 的 c ++
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<cmath>
#define inf 0xffffffff
#define lowbit(i) ((i)&(-(i)))
using namespace std;
const int maxn=100861;
stack<int> s;
int c[maxn];
void update(int x, int v)
{
for(int i=x; i<maxn; i+=lowbit(i))
c[i]+=v;
}
int sum(int x)
{
int t=0;
for(int i=x; i>=1; i-=lowbit(i))
t+=c[i];
return t;
}
void find()
{
int left=0;
int right=maxn;
int mid;
int k=(s.size()+1)>>1;
while(left<right)
{
mid=(left+right)>>1;
if(sum(mid)>=k)
right=mid;
else
left=mid+1;
}
printf("%d\n", left);
}
int main()
{
int n;
scanf("%d", &n);
char command[15];
for(int i=0; i<n; i++)
{
scanf("%s", command);
if(command[1]=='u')
{
int t;
scanf("%d", &t);
update(t, 1);
s.push(t);
}
else
if(command[1]=='o')
if(s.size()==0)
printf("Invalid\n");
else
{
update(s.top(), -1);
printf("%d\n", s.top());
s.pop();
}
else
if(command[1]=='e')
if(s.size()==0)
printf("Invalid\n");
else
find();
}
return 0;
}