pat 甲级 1057 Stack(30) (树状数组+二分)
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 (≤105). 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 105.
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 Invalid
instead.
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) (树状数组+二分)的更多相关文章
- PAT甲级1057 Stack【树状数组】【二分】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...
- PAT甲级题解-1057. Stack (30)-树状数组
不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekM ...
- 1057. Stack (30) - 树状数组
题目如下: Stack is one of the most fundamental data structures, which is based on the principle of Last ...
- PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the prin ...
- PAT 1057 Stack [难][树状数组]
1057 Stack (30)(30 分) Stack is one of the most fundamental data structures, which is based on the pr ...
- PAT-1057 Stack (树状数组 + 二分查找)
1057. Stack Stack is one of the most fundamental data structures, which is based on the principle of ...
- PAT甲级1057. Stack
PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...
- 树状数组+二分||线段树 HDOJ 5493 Queue
题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...
- 牛客多校第3场 J 思维+树状数组+二分
牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...
随机推荐
- 2019java学习路线图
学习路线图往往是学习一样技术的入门指南.网上搜到的Java学习路线图也是一抓一大把.但是很多学习路线图总结的云里雾里,也没有配套的视频,学习效果并不好. 分享一个完整的Java学习路线图给大家,也是贴 ...
- django静态文件配置和使用
一.首先需要了解的知识点是: 1.出于对效率和安全的考虑,django管理静态文件的功能仅限于在开发阶段的debug模式下使用,且需要在配置文件的INSTALLED_APPS中加入django.con ...
- CentOS7.5 安装MySql教程
CentOS7位安装MySql教程 1.先检查系统是否装有mysql rpm -qa | grep mysql 2.下载mysql的repo源 wget http://repo.mysql.com/m ...
- Centos7.3 为php7 安装swoole 扩展
今天心血来潮想在服务器上安装一下swoole扩展 下面列一下教程: xshell进入你的服务器 然后目录自选吧 反正我放在根目录了 下面是扩展链接: wget https://github.co ...
- (十)Activitivi5之启动流程/完成任务的时候设置流程变量
一.启动流程的时候设置流程变量 1.1 案例 /** * 启动流程实例 */ @Test public void start() { Student student=new Student(); st ...
- Uwl.Admin开源框架(二)
Uwl.Admin开源框架基于QuartzNet定时任务模块的实现 Quartz.NET官网地址:https://www.quartz-scheduler.net/ Quartz.NET文档地址:ht ...
- sql 时间获取
现在时间:GETDATE() 昨天时间:CONVERT(VARCHAR(16),DATEADD(d,-1,GETDATE()),120)
- Genymotion上运行ARM架构Android项目
问题 Genymotion是x86的模拟器,在集成一些第三方的SDK时需要添加一些放在armeabi.armeabi-64文件夹下面的.so文件,这些文件在arm架构下才能运行.这时模拟器就不能用了. ...
- Python函数知识点总结
1.函数的定义2.如何定义一个函数以及函数语法3.函数的调用4.函数的参数(形参,实参)以及参数的传递5.函数的返回值6.变量的作用域7.匿名函数8.嵌套函数和闭包9.装饰器10.函数思维导图 1.函 ...
- Vue路由参数
vue路由参数 1.参数router-link vue.prototype.xxx = {add:fn}`所有组件中,使用this.xxx就能拿到这个对象2.查询字符串 (1)配置: :to=&quo ...