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 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) (树状数组+二分)的更多相关文章
- 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.访问 ...
随机推荐
- SAS学习笔记29 logistic回归
变量筛选 当对多个自变量建立logistic回归模型时,并不是每一个自变量对模型都有贡献.通常我们希望所建立的模型将具有统计学意义的自变量都包含在内,而将没有统计学意义的自变量排除在外,即进行变量筛选 ...
- 三种redis数据导出导入方式
推荐博客链接:https://www.cnblogs.com/hjfeng1988/p/7146009.html https://blog.csdn.net/qq_14945847/article/d ...
- Swarm系列7--存储介绍
存储介绍 1. 存储使用 与docker一样,在使用swarm服务级别的时候可以定义服务的存储需求, docker存储介绍参考: Docker之应用数据管理(volume/bind mount/tmp ...
- Apache2.4+Tomcat7.0整合配置详解
一.简单介绍 Apache.Tomcat Apache HTTP Server(简称 Apache),是 Apache 软件基金协会的一个开放源码的网页服务器,可以在 Windows.Unix.Lin ...
- 针对IE6 7 8当独写样式
IE8的格式: .foot{padding:12px 10px\9;} //在后面加\9 IE7的格式: .foot{*padding:12px 10px\9;} //在前面加* IE6的格式: .f ...
- java轻松玩转localdatetime
废话不多说,直接上代码 //时间戳转LocalDateTime public static LocalDateTime getLocalDateTime(long timestamp) { Insta ...
- 【数字图像处理】帧差法与Kirsch边缘检测实现运动目标识别与分割
本文链接:https://blog.csdn.net/qq_18234121/article/details/82763385 作者:冻人的蓝鲸梁思成 视频分割算法可以从时域和空域两个角度考虑.时域分 ...
- 【python】python3连接mysql数据库
一.安装pymysql 详见http://www.runoob.com/python3/python3-mysql.html 二.连接mysql数据库 db = pymysql.connect( #连 ...
- sql sever 查询用户所有的表和各个表数据量
和oracle有区别, 需要关联表 SELECT A.NAME ,B.ROWS FROM sysobjects A JOIN sysindexes B ON A.id = B.id WHERE A ...
- Python处理异常
异常操作: 1.错误的定义和特征 什么是错误:错误是由于逻辑和语法等导致一个程序无法正常执行的问题 错误的特征:有些错误无法预知 2.异常的定义 异常是程序错误时表示的一种状态 异常发生时,程序不会再 ...