# L3-002 特殊堆栈 (30分)（四种方法）

Push key
Pop
PeekMedian

17
Pop
PeekMedian
Push 3
PeekMedian
Push 2
PeekMedian
Push 1
PeekMedian
Pop
Pop
Push 5
Push 4
PeekMedian
Pop
Pop
Pop
Pop

Invalid
Invalid
3
2
2
1
2
4
4
5
3
Invalid

1.构建两个堆，形成双顶堆， 其中顶堆中的数都大于底堆中的数

#include <iostream>
#include <cstring>
#include <set>
#include <stack>

using namespace std;

stack<int> stk;
multiset<int> up, down;//允许有重复元素的两个小顶堆， 且up中每个元素都大于down中的元素

{
while (up.size() > down.size()) //如果顶堆的元素个数大于底堆
{
down.insert(*up.begin());
up.erase(up.begin());
}

while (down.size() > up.size() + 1)
{
auto it = down.end();
it -- ;
up.insert(*it);
down.erase(it);
}
}

int main()
{
int n;
scanf("%d", &n);
char op[20];
while (n -- )
{
scanf("%s", op);
if (strcmp(op, "Push") == 0)
{
int x;
scanf("%d", &x);
stk.push(x);
if (up.empty() || x < *up.begin()) down.insert(x);// 如果up为空或者其小于up中最小的元素则插入down中
else up.insert(x);
}
else if (strcmp(op, "Pop") == 0)
{
if (stk.empty()) puts("Invalid");
else
{
int x = stk.top();
stk.pop();
printf("%d\n", x);
auto it = down.end(); //底堆中的的最后一个指针
//值得注意的是， 在set中end（）存放的是set中元素的个数
//end() - 1才是最后一个元素
it -- ;
if (x <= *it) down.erase(down.find(x));//set中也有find
else up.erase(up.find(x));

}
}
else
{
if (stk.empty()) puts("Invalid");
else
{
auto it = down.end();
it -- ;
printf("%d\n", *it);
}
}
}

return 0;
}



#include<bits/stdc++.h>

using namespace std;
#define lowbit(x) x&-x
const int N = 1e5 + 10;

int s[N], top, pre;
int t[N];

void update(int x, int num)
{
for(int i = x; i < N; i += lowbit(i))
{
t[i] += num;
}
}

int getsum(int x)
{
int sum = 0;
for(int i = x; i; i -= lowbit(i))
sum += t[i];
return sum;
}

int query(int x)
{
x -= 1;
if(x % 2 == 1)
x += 1;
x /= 2;
int l = 1, r = N - 1;
while(l < r)
{
int mid = l + r >> 1;
if(getsum(mid) >= x)
r = mid;
else
l = mid + 1;
}
return l;
}
int main()
{
int t;
cin >> t;
pre = top = 1;
while(t --)
{
string s1;
cin >> s1;
if(s1 == "Pop")
{
if(top == 1)
cout << "Invalid" << endl;
else
{
top --;
update(s[top], -1);
cout << s[top] << endl;
}
}
else if(s1 == "PeekMedian")
{
if(top == 1)
{
cout << "Invalid" << endl;
}
else
{
int x = top;
cout << query(x) << endl;
}
}
else
{
int x;
cin >> x;
s[top ++] = x;
update(x, 1);
}
}
return 0;
}



#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
//Constant area
const int MAXN=1000000;
const int MIN_INF=0x80000000;
const int MAX_INF=0x7fffffff;
//Variable area
vector<int> v;
stack<int> s;
int n,tmp;
//Initialization area
void init(){
v.clear();
return;
}
//Function area

int main(){
ios::sync_with_stdio(false);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
init();
cin>>n;
while(n--){
if(v.size()==0){
cout<<"Invalid"<<endl;
continue;
}
auto i=lower_bound(v.begin(),v.end(),s.top());
cout<<*i<<endl;
v.erase(i);
s.pop();
if(v.size()==0){
cout<<"Invalid"<<endl;
continue;
}
int mid=v.size()%2? v.size()/2:v.size()/2-1;
cout<<v[mid]<<endl;
cin>>tmp;
auto i=lower_bound(v.begin(),v.end(),tmp);
v.insert(i,tmp);
s.push(tmp);
}
}
}



#include<bits/stdc++.h>

using namespace std;
#define lowbit(x) x&-x
const int N = 1e5 + 10;

int s[N], top, pre;
int t[N << 2];

void update(int l, int r, int x, int num, int pos)
{
if(l == r)
{
t[pos] += num;
return;
}
int mid = l + r >> 1;
if(mid >= x) update(l, mid, x, num, pos<<1);
else update(mid + 1, r, x, num, pos<<1|1);
t[pos] = t[pos<<1] + t[pos<<1|1];
}

int query(int l, int r, int y, int pos)
{
if(l == r)
return l;
int mid = l + r >> 1;
if(y <= t[pos<<1])
return query(l, mid, y, pos<<1);
else
return query(mid + 1, r, y - t[pos<<1], pos<<1|1);
}
int main()
{
int t;
cin >> t;
pre = top = 1;
while(t --)
{
string s1;
cin >> s1;
if(s1 == "Pop")
{
if(top == 1)
cout << "Invalid" << endl;
else
{
top --;
update(1, N - 1, s[top], -1, 1);
cout << s[top] << endl;
}
}
else if(s1 == "PeekMedian")
{
if(top == 1)
{
cout << "Invalid" << endl;
}
else
{
int x = top;
x -= 1;
if(x % 2 == 1)	x += 1;
x /= 2;
cout << query(1, N - 1, x, 1) << endl;
}
}
else
{
int x;
cin >> x;
s[top ++] = x;
update(1, N - 1, x, 1, 1);
}
}
return 0;
}


02-25 3622

03-30 140
11-17 285
01-23 139