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 N is 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 "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 <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define MAX 100000
using namespace std; int n,d,s[MAX],tree[MAX + ],c;
char ope[];
int lowbit(int t) {
return t&-t;
}
void update(int x,int y) {
while(x <= MAX) {
tree[x] += y;
x += lowbit(x);
}
}
int getsum(int x) {
int ans = ;
while(x > ) {
ans += tree[x];
x -= lowbit(x);
}
return ans;
}
void printM() {
int m = (c % ? c / + : c / );///中间个数应该是第几个数
int l = ,r = MAX,mid;
while(l < r) {
mid = (l + r) / ;
if(getsum(mid) < m)l = mid + ;
else r = mid;
}
printf("%d\n",l);
}
int main() {
scanf("%d",&n);
while(n --) {
scanf("%s",ope);
if(ope[] == 'u') {
scanf("%d",&d);
update(s[c ++] = d,);
}
else if(ope[] == 'o') {
if(!c) {
printf("Invalid\n");
}
else {
update(s[-- c],-);
printf("%d\n",s[c]);
}
}
else {
if(!c) {
printf("Invalid\n");
}
else {
printM();
}
}
}
}
个数对应中间位置的最小的。

1057 Stack (30)(30 分)的更多相关文章

  1. pat 甲级 1057 Stack(30) (树状数组+二分)

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

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

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

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

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

  4. PAT甲级1057. Stack

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

  5. etectMultiScale(gray, 1.2,3,CV_HAAR_SCALE_IMAGE,Size(30, 30))

    # 函数原型detectMultiScale(gray, 1.2,3,CV_HAAR_SCALE_IMAGE,Size(30, 30)) # gray需要识别的图片 # 1.03:表示每次图像尺寸减小 ...

  6. c# 时间格式处理,获取格式: 2014-04-12T12:30:30+08:00

    C#  时间格式处理,获取格式: 2014-04-12T12:30:30+08:00 一.获取格式: 2014-04-12T12:30:30+08:00 方案一:(局限性,当不是当前时间时不能使用) ...

  7. java.time.format.DateTimeParseException: Text '2019-10-11 12:30:30' could not be parsed at index 10

    java.time.format.DateTimeParseException: Text '2019-10-11 12:30:30' could not be parsed at index 10 ...

  8. PAT乙级:1057 数零壹 (20分)

    PAT乙级:1057 数零壹 (20分) 题干 给定一串长度不超过 105 的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一 ...

  9. 【PAT甲级】1057 Stack (30 分)(分块)

    题意: 输入一个正整数N(<=1e5),接着输入N行字符串,模拟栈的操作,非入栈操作时输出中位数.(总数为偶数时输入偏小的) trick: 分块操作节约时间 AAAAAccepted code: ...

随机推荐

  1. 关于ES6的let,const与var之间的三生三世

    首先需要明确的是let.const.var都是用来定义变量的 在ES6之前,我们一般都用var来定义变量,例如 : function test(){ var i=1; console.log(i); ...

  2. Android 禁止状态栏下拉

    同学项目用到Android 禁止状态栏下拉,我也迷茫,网上很多资料都不行,最终找到了下面一篇博客,感觉很不错,说的比较详细,供大家参考了 http://blog.csdn.net/u011913612 ...

  3. Elipse 导入项目出现问题

    1.通常出现jsp页面报错 那是因为server没有绑定 build path ->apache-tomcat ->edit 2.target runtime apache tomcat ...

  4. python函数的作用域

    python函数的作用域 以下内容参考自runoob网站,以总结python函数知识点,巩固基础知识,特此鸣谢! 原文地址:http://www.runoob.com/python3/python3- ...

  5. 九度OJ 1197:奇偶校验 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3590 解决:1511 题目描述: 输入一个字符串,然后对每个字符进行奇校验,最后输出校验后的二进制数(如'3',输出:10110011). ...

  6. 右键打开cmd

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shell\OpenCmdHere]@="Open cmd ...

  7. 【译】理解Spring MVC Model Attribute 和 Session Attribute

    作为一名 Java Web 应用开发者,你已经快速学习了 request(HttpServletRequest)和 session(HttpSession)作用域.在设计和构建 Java Web 应用 ...

  8. java.time.format.DateTimeFormatter

    Java的日期与时间 DateTimeFormatter类是Java 8中日期时间功能里,用于解析和格式化日期时间的类,位于java.time.format包下.   1.预定义的DateTimeFo ...

  9. 洛谷 2233 [HNOI2002]公交车路线

    题目戳这里 一句话题意 一个大小为8的环,求从1到5正好n步的方案数(途中不能经过5). Solution 巨说这个题目很水 应该是比较容易的DP,直接从把左边和右边的方案数加起来即可,但是有几个需要 ...

  10. 将本地jar包手动复制到Maven库中,在其它电脑上用Maven打包时出错

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/UP19910522/article/details/31396107 背景交代:在做图片水印时候引入 ...