大家都知道“堆栈”是一种“先进后出”的线性结构,基本操作有“入栈”(将新元素插入栈顶)和“出栈”(将栈顶元素的值返回并从堆栈中将其删除)。现请你实现一种特殊的堆栈,它多了一种操作叫“查中值”,即返回堆栈中所有元素的中值。对于N个元素,若N是偶数,则中值定义为第N/2个最小元;若N是奇数,则中值定义为第(N+1)/2个最小元。

输入格式:

输入第一行给出正整数N(<= 105)。随后N行,每行给出一个操作指令,为下列3种指令之一:

Push key
Pop
PeekMedian

其中Push表示入栈,key是不超过105的正整数;Pop表示出栈;PeekMedian表示查中值。


输出格式:

对每个入栈指令,将key入栈,并不输出任何信息。对每个出栈或查中值的指令,在一行中打印相应的返回结果。若指令非法,就打印“Invalid”。

输入样例:

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 Pop和Push都好操作,可以用个数组维持一个栈,关键是求第k小的值(k = n / 2 || k = (n + 1)/ 2),栈的元素个数是变化的,线段树不太会用,所以用树状数组来记录,查找的时候用二分法查找。
代码:
#include <bits/stdc++.h>
using namespace std;
int t[],m;
int lowbit(int t)
{
return t&-t;
}
void update(int x,int y)
{
for(;x <= ;x += lowbit(x))
{
t[x] += y;
}
}
int getsum(int x)
{
int sum = ;
for(;x > ;x -= lowbit(x))
{
sum += t[x];
}
return sum;
}
int query(int x)
{
int l = ,r = m,mid,sum;
while(l < r)///如果从1到mid一共不到x个数,就让l = mid + 1,但是如果大于或等于x都有可能,所以此时选最左边的值(即query(t)<x && query(t + 1)>=x 选t + 1),所以r = mid 不能让r = mid - 1,不然取的可能不是upper的值
{
mid = (l + r) / ;
sum = getsum(mid);
if(sum >= x)r = mid;
else l = mid + ;
}
//l == r
return l;
}
int main()
{
char s[];
int n,st[],c = ;
scanf("%d",&n);
for(int i = ;i < n;i ++)
{
scanf("%s",s);
if(s[] == 'u')
{
scanf("%d",&st[c]);
if(st[c] > m)m = st[c];
update(st[c ++],);
}
else if(!c)
{
puts("Invalid");
}
else
{
if(s[] == 'o')
{
printf("%d\n",st[-- c]);
update(st[c],-);
}
else
{
if(c % )printf("%d\n",query((c + ) / ));
else printf("%d\n",query(c / ));
}
}
}
}

L3-002 特殊堆栈 (30 分)的更多相关文章

  1. L3-002 特殊堆栈 (30分) vector容器的模拟、vector容器的一些用法

    vector容器的简单应用,我们可以用vector维护一个有序数组,每次对要插入的数用upper_bound或者lower_bound来 为这个数找一个应该插入到vector的位置.另外再找一个数组来 ...

  2. PTA 07-图5 Saving James Bond - Hard Version (30分)

    07-图5 Saving James Bond - Hard Version   (30分) This time let us consider the situation in the movie ...

  3. PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

    1057 Stack (30 分)   Stack is one of the most fundamental data structures, which is based on the prin ...

  4. PTA 社交网络图中结点的“重要性”计算(30 分)

    7-12 社交网络图中结点的“重要性”计算(30 分) 在社交网络中,个人或单位(结点)之间通过某些关系(边)联系起来.他们受到这些关系的影响,这种影响可以理解为网络中相互连接的结点之间蔓延的一种相互 ...

  5. L3-015 球队“食物链” (30 分)

    L3-015 球队“食物链” (30 分)   某国的足球联赛中有N支参赛球队,编号从1至N.联赛采用主客场双循环赛制,参赛球队两两之间在双方主场各赛一场. 联赛战罢,结果已经尘埃落定.此时,联赛主席 ...

  6. PAT A1127 ZigZagging on a Tree (30 分)——二叉树,建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  7. 04-树6 Complete Binary Search Tree(30 分)

    title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...

  8. PTA 7-2 二叉搜索树的结构(30 分)

    7-2 二叉搜索树的结构(30 分) 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大 ...

  9. 1127 ZigZagging on a Tree (30 分)

    1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...

  10. 【PAT】1053 Path of Equal Weight(30 分)

    1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight W​i​​ assigned t ...

随机推荐

  1. 一款基于jQuery Ajax的等待效果

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  2. code first System.Data.Entity.Infrastructure.CommitFailedException: An error was reported while committing a database transaction but it could not be determined whether the transaction succeeded

    System.Data.Entity.Infrastructure.CommitFailedException: An error was reported while committing a da ...

  3. Sqlserver 创建账号

    下面是通过脚本创建账号,创建一个appuser 的账号,密码:123456,可操作的DB:TEST 赋予权限,增删改查,操作视图,存储过程.当然当前的账号要有足够的权限. create login a ...

  4. 使用discriminator实现鉴别器

    1在人员接口实现方法 public Employee getEmpByIdStep(Integer id); 2在映射文件进行配置 <!-- public Employee getEmpById ...

  5. 【工具安装】BurpSuite 安装教程

    日期:2019-07-14 17:23:53 介绍:安装 JDK,配置 JDK 的环境变量.安装 BurpSuite,抓包 0x01. 安装 JDK 安装 JDK BurpSuite 需要 JAVA ...

  6. visualSVN提交强制添加注释

    Visual SVN Server下 右键项目  “所有任务”>“Manage Hooks” >选中Pre-commit hook然后edit编辑,添加如下代码 @echo off set ...

  7. docker 一些命令

    docker的基本命令 (1)创建一个虚拟机:docker-machine create --driver virtualbox default, (2)列出所有虚拟机:docker-machine ...

  8. Linux cat 多行写入文件防止变量替换

    Linux cat 多行写入文件防止变量替换  问题描述 对多个变量及多行输出到文件,存在变量自动替换,当使用cat<<EOF不想对内容进行变量替换.命令替换.参数展开等 问题解决 转义特 ...

  9. Centos7 搭建pptp服务器

    1.检查是否支持pptp 返回ok即表示支持 modprobe ppp-compress-18 && echo ok 2.安装ppp yum install -y ppp 3.安装pp ...

  10. kafka生产者java客户端

    producer 包含一个用于保存待发送消息的缓冲池,缓冲池中消息是还没来得及传输到kafka集群的消息. 位于底层的kafka I/O线程负责将缓冲池中的消息转换成请求发送到集群.如果在结束prod ...