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 (-th smallest element if N is even, or (-th if N is odd.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤). 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 1.
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
我认为树状数组只是用来记录前缀和的 但换个思路来想 把元素当作数组下标 出现的次数当作该下标对应的值
树状数组记录了 对应区间的数的个数 不仅让查找方便 增加元素个数也变得方便了
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
const int maxn = ;
int C[];
stack<int> S;
int lowbit(int num)
{
return num & (-num);
}
int getSum(int x)
{
int sum = ;
while (x)
{
sum += C[x];
x -= lowbit(x);
}
return sum;
}
void Update(int x, int value)
{
while (x<maxn)
{
C[x] += value;
x += lowbit(x);
}
}
void PeekMedian()
{
int left = , right = maxn;
int k = (S.size() + ) / ;
while (left<right)
{
int mid = (left + right) / ;
if (getSum(mid)>= k)
right = mid;
else
left = mid+;
}
cout <<left<< endl;
}
int main()
{
int N;
cin >> N;
for (int i = ; i < N; i++)
{
string s;
cin >> s;
if (s == "Push")
{
int i;
cin >> i;
S.push(i);
Update(i, );
}
else if (s == "Pop")
{
if (!S.size())
cout << "Invalid" << endl;
else
{
Update(S.top(), -);
cout << S.top()<<endl;
S.pop();
}
}
else {
if (!S.size())
cout << "Invalid" << endl;
else
PeekMedian();
}
}
}
1057 Stack (30分)(树状数组+二分)的更多相关文章
- PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the prin ...
- 树状数组+二分||线段树 HDOJ 5493 Queue
题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...
- TZOJ 4602 高桥和低桥(二分或树状数组+二分)
描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...
- 牛客多校第3场 J 思维+树状数组+二分
牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- POJ 2182 Lost Cows 【树状数组+二分】
题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]
题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...
- The Stream of Corning 2( 权值线段树/(树状数组+二分) )
题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...
- PAT-1057 Stack (树状数组 + 二分查找)
1057. Stack Stack is one of the most fundamental data structures, which is based on the principle of ...
随机推荐
- Python魔法方法之 __call__
前言 Python的魔法方法是指Python内部已经包含的,被双下划线所包围的方法,这些方法在特定的操作时会自动被调用.魔法方法可以使Python的自由度变得更高,当不重载魔法方法时它可以在规定的默认 ...
- ggplot2(1) 简介
1.1 简介 ggplot2是一个用来绘制统计图形(数据图形)的R软件包,与其他大多数的图形软件包不同,ggplot2是由其背后的一套图形语法所支持的.ggplot2可以绘制出很多美观度的图形,同时能 ...
- 前端构建工具gulpjs的使用介绍及技巧【转载】
转载至:http://www.cnblogs.com/2050/p/4198792.html gulpjs是一个前端构建工具,与gruntjs相比,gulpjs无需写一大堆繁杂的配置参数,API也非常 ...
- express第三方中间件研究之bodyParser中间件
转载至:http://www.cnblogs.com/lianer/p/5178693.html 接触nodejs已有一段时间了,但最近才开始落实项目,于是使用express应用生成器生成了一个应用. ...
- seldom之数据驱动
seldom之数据驱动 如果自动化某个功能,测试数据不一样而操作步骤是一样的,那么就可以使用参数化来节省测试代码. seldom是我在维护一个Web UI自动化测试框,这里跟大家分享seldom参数化 ...
- SpringCloud学习系列<一>版本介绍
SpringCloud学习踩坑记<一> SpringCloud版本迭代实在太快,学习起来也是各种坑,博主用的是"当前"的最新版本,借鉴周立老大的Spring Cloud ...
- JAVA系列-JVM
1)JVM体系概述 JVM体系结构概览 JVM参数调优及相关参数 JVM的参数类型 标配参数:java-version; java -help X参数:-Xint 解释执行: -Xcomp 第一次使 ...
- for循环与串行化、并行化Stream流性能对比
第四章 并行化Stream流 关注公众号(CoderBuff)回复"stream"获取<Java8 Stream编码实战>PDF完整版. <Java8 Strea ...
- Dapper操作MySQL数据库获取JSON数据中文乱码
前言 在项目中利用Dapper将JSON数据存储到MySQL数据库,结果发现JSON数据中的中文乱码,特此记录,希望对存储JSON的童鞋能有所帮助,文中若有错误之处,还望批评指正. Dapper获取J ...
- Vue-Cli4笔记
Vue-Cli4与Vue-Cli2区别浅谈 当时学习 Vue-Cli 的时候看的是 Vue-Cli2 的相关教程,当把 package.json 上传 github 的时候提醒有安全问题,于是准备使用 ...