PAT 甲级 1057 Stack
https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592
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
代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
int N;
int c[maxn];
stack<int> s; int lowerbit(int x) {
return x & -x;
} void update(int x, int v) {
for(int i = x; i < maxn; i += lowerbit(i))
c[i] += v;
} int getsum(int x) {
int sum = 0;
for(int i = x; i >= 1; i -= lowerbit(i))
sum += c[i];
return sum;
} void PeekMedian() {
int left = 1, right = maxn, mid, k = (s.size() + 1) / 2;
while(left < right) {
mid = (left + right) / 2;
if(getsum(mid) >= k)
right = mid;
else left = mid + 1;
}
printf("%d\n", left);
} int main() {
scanf("%d", &N);
while(N --) {
string op;
cin >> op;
if(op == "Pop") {
if(s.empty()) printf("Invalid\n");
else {
printf("%d\n", s.top());
update(s.top(), -1);
s.pop();
}
} else if(op == "Push") {
int num;
scanf("%d", &num);
s.push(num);
update(num, 1);
} else {
if(s.empty()) printf("Invalid\n");
else PeekMedian();
}
}
return 0;
}
树状数组 第一次写到树状数组 看博客还没看的很懂但是隐约感觉到这是一个模板题 emmmm 以后多做一点就会明白了叭 唉 哭唧唧
PAT 甲级 1057 Stack的更多相关文章
- PAT甲级1057. Stack
PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...
- 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(30) (树状数组+二分)
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...
- PAT甲级1057 Stack【树状数组】【二分】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...
- PAT甲级——A1057 Stack
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
- PAT 1057 Stack [难][树状数组]
1057 Stack (30)(30 分) Stack is one of the most fundamental data structures, which is based on the pr ...
- pat甲级题解(更新到1013)
1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
随机推荐
- opencv7-ml之统计模型
在opencv的ml模块中有个统计模型类,而其他的比如朴素贝叶斯分类器.knn.svm等等其他模型都是基于该模型上派生出来的.所以先介绍下该模型. 该类的定义在文件"opencv\sourc ...
- golang 文件服务器
在go语言中可以用一句代码做一个文件服务器.如果有很多文件需要通过网页来供其他人下载,可以使用这个方法. package main import ( "log" "net ...
- jquery操作checked
jquery操作checkbox,如何获取勾选状态?如何使得勾选?如何取消勾选? 来段代码就知道了: <html> <head> <meta charset=" ...
- USB主机控制器ECHI
USB主机控制器ECHI 2017年10月24日 15:44:11 阅读数:239 1. 主机控制器(Host Controller) • UHCI: Universal Host Controlle ...
- 20155227《网络对抗》Exp2 后门原理与实践
20155227<网络对抗>Exp2 后门原理与实践 基础问题回答 (1)例举你能想到的一个后门进入到你系统中的可能方式? 在非官方网站下载软件时,后门很可能被捆绑在软件中. 攻击者利用欺 ...
- 使用HibernateDaoSupport抽取BaseDao
在开发采用Struts2+Spring+hibernate这三大框架的项目时,我们需要一个抽取一个BaseDao.这个Dao里面CRUD都给封装好,后续的其他Dao直接用它的功能就可以 ...
- MFC Edit控件的使用~~
EditBox,一般用于显示数字文本,或者与用户沟通获取数字文本. 这里介绍一种将EditBox与一个变量关联起来的方法: 快捷键:Shift + Ctrl + X,进入类导向,选择成员变量属性页: ...
- wordpress必装的插件 wp最常用的十个插件
wordpress是世界上著名的博客系统,简称wp.一般新安装完wordpress以后,往往需要首先安装一些插件,这样才可以使用wordpress的更多功能.wp最常用的十个插件有哪些呢,可能根据每个 ...
- ZeroMQ使用汇总
ZeroMQ,史上最快的消息队列 —– ZMQ的学习和研究 ZeroMQ 的模式 [架构] ZeroMQ 深度探索(一) 消息队列ZeroMQ 服务端使用流程: void* m_Context; v ...
- 【SP1812】LCS2 - Longest Common Substring II
[SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...