1057 Stack (30 分)

Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). Now you are supposed to implement a stack with an extra operation: PeekMedian -- return the median value of all the elements in the stack. With N elements, the median value is defined to be the (N/2)-th smallest element if N is even, or ((N+1)/2)-th if Nis odd.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤10​5​​). Then N lines follow, each contains a command in one of the following 3 formats:

Push key
Pop
PeekMedian

where key is a positive integer no more than 10​5​​.

Output Specification:

For each Push command, insert key into the stack and output nothing. For each Pop or PeekMedian command, print in a line the corresponding returned value. If the command is invalid, print Invalidinstead.

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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int st[100005];
ll bit[1000005];
ll n;
void add(ll i,ll x)
{
while(i<=n)
{
bit[i]+=x;
i+=i&-i;
}
}
ll sum(ll i)
{
ll s=0;
while(i>0)
{
s+=bit[i];
i-=i&-i;
}
return s;
}
int pos=0;
int main()
{
n=100000;
//freopen("in.txt","r",stdin);
int N;
cin>>N;
string op;
while(N--)
{
cin>>op;
if(op[1]=='o')
{
if(pos==0){cout<<"Invalid"<<endl;continue;}
cout<<st[pos]<<endl;
add(st[pos],-1);
pos--;
}
else if(op[1]=='u')
{
int num;
cin>>num;
add(num,1);
st[++pos]=num;
}
else if(op[1]=='e')
{
if(pos==0){cout<<"Invalid"<<endl;continue;}
int x=(pos%2==0)?pos/2:(pos+1)/2;
int lb=0;int ub=100001;
while(ub-lb>1)
{
int mid=(ub+lb)/2;
if(sum(mid)<x)lb=mid;
else ub=mid;
}
cout<<ub<<endl;
}
else {
cout<<"Invalid"<<endl;continue;
}
}
return 0;
}

pat 甲级 1057 Stack(30) (树状数组+二分)的更多相关文章

  1. PAT甲级1057 Stack【树状数组】【二分】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...

  2. PAT甲级题解-1057. Stack (30)-树状数组

    不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekM ...

  3. 1057. Stack (30) - 树状数组

    题目如下: Stack is one of the most fundamental data structures, which is based on the principle of Last ...

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

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

  5. PAT 1057 Stack [难][树状数组]

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

  6. PAT-1057 Stack (树状数组 + 二分查找)

    1057. Stack Stack is one of the most fundamental data structures, which is based on the principle of ...

  7. PAT甲级1057. Stack

    PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...

  8. 树状数组+二分||线段树 HDOJ 5493 Queue

    题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...

  9. 牛客多校第3场 J 思维+树状数组+二分

    牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...

随机推荐

  1. Golang不会自动把slice转换成interface{}类型的slice

    目录 例子 原因 如何去实现 例子 我们时常会写一些interface,例如: type A interface{ Print() } type B struct { } func (b *B) Pr ...

  2. Geometers Anonymous Club CodeForces - 1195F (闵可夫斯基和)

    大意: 给定$n$个凸多边形, $q$个询问, 求$[l,r]$内闵可夫斯基区间和的顶点数. 要用到一个结论, 闵可夫斯基和凸包上的点等于向量种类数. #include <iostream> ...

  3. 解决GitHub下载速度缓慢的问题

    随着微软大大宣布GitHub针对个人用户的仓库免费,相信每位开发者都感受到了"真香". 然而因为一些众所周知的原因,国内访问GitHub总会遇到下载速度缓慢.链接意外终止的情况. ...

  4. (三)easyUI之树形组件

    一.同步树 1.1 概念 所有节点一次性加载完成 1.2 案例 1.2.1 数据库设计 1.2.2 编码 index.jsp <%@ page language="java" ...

  5. Visual Stdio C++ 编译常见问题

    1. new 数组出现崩溃 new 数组时数组下标出现负值,但未做出错处理: new数组,数组字节数大于4MB的时候有可能出现crash! 解决办法: 加入 try  catch 后,这样的错误几乎没 ...

  6. JS实现异步的几种方式

    1.JS执行环境:单线程   单线程:就是指一次只能完成一件任务.若有多个任务时,就必须排队,等前面一个任务完成之后,再执行后面一个任务 缺点:任务耗时很长,后面的任务需要等待,拖延整个程序的执行.例 ...

  7. BPM软件_财务报销流程管理解决方案_K2工作流引擎

    财务报销,对任何企业都是日常运营中重要的一个环节.但报销流程周期长,反复签字手续繁杂,报销过程不透明 ,单据归档保存.检索困难等问题也让员工头疼.为了解决这些困扰,财务报销流程电子化一时成为热门之选. ...

  8. git基本命令总结

    介绍 上一篇博客介绍了git的基本使用方式,建议可以去阅读一下廖雪峰关于git的文章写的十分详细,并且通俗易懂,这篇博客主要是总结上一篇博客中用到的git命令,方便使用查询. git常用命令小结 gi ...

  9. MySQL之日期时间函数

      1.NOW() 用法:显示当前日期和时间 举例: mysql> select NOW(); +---------------------+ | NOW() | +-------------- ...

  10. h5 移动端开发自适应 meta name="viewport"的使用总结

    本文系个人理解,可能有误差,仅供参考,谨慎采纳! 布局视口: 系统自带 一般大于屏幕宽度 理想宽度:  设置页面的viewport 的一个宽度,使不同的手机的布局视口宽度尽量接近可视窗口的值: 可视视 ...