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

题目大意:现请你实现一种特殊的堆栈,它多了一种操作叫“查中值”,即返回堆栈中所有元素的中值。对于N个元素,若N是偶数,则中值定义为第N/2个最小元;若N是奇数,则中值定义为第(N+1)/2个最小元。
分析:用排序查询的方法会超时~~用树状数组,即求第k = (s.size() + 1) / 2大的数。查询小于等于x的数的个数是否等于k的时候用二分法更快~

AC代码:

#include<iostream>
#include<stack>
#define lowbit(i) ((i) & (-i))
const int maxn=;
using namespace std;
int c[maxn];
stack<int>s;
void update(int x, int v) {
for(int i = x; i < maxn; i += lowbit(i))
c[i] += v;//值为i的数出现并更新与其相关的数
}
int getsum(int x) {
int sum = ;
for(int i = x; i >= ; i -= lowbit(i))
sum += c[i];
return sum;
}
void PeekMedian() {//查询小于等于x的数的个数是否等于k的时候用二分法更快~
int left = , right = maxn, mid, k = (s.size() + ) / ;
while(left < right) {
mid = (left + right) / ;
if(getsum(mid) >= k)//每次用getsum(i)求得前i个数中实际出现了几个数,与中位数k比较即可
right = mid;
else
left = mid + ;
}
printf("%d\n", left);
}
int main() {
int n, temp;
scanf("%d", &n);
char str[];
for(int i = ; i < n; i++) {
scanf("%s", str);
if(str[] == 'u') {
scanf("%d", &temp);
s.push(temp);
update(temp, );
} else if(str[] == 'o') {
if(!s.empty()) {
update(s.top(), -);
printf("%d\n", s.top());
s.pop();
} else {
printf("Invalid\n");
}
} else {
if(!s.empty())
PeekMedian();
else
printf("Invalid\n");
}
}
return ;
}

PAT 甲级1057 Stack (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

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

  3. PAT甲级1057 Stack【树状数组】【二分】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...

  4. PAT 甲级 1057 Stack

    https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 Stack is one of the mo ...

  5. 1057 Stack (30分)(树状数组+二分)

    Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...

  6. PAT-1057 Stack (树状数组 + 二分查找)

    1057. Stack Stack is one of the most fundamental data structures, which is based on the principle of ...

  7. PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)

    1147 Heaps (30 分)   In computer science, a heap is a specialized tree-based data structure that sati ...

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

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

  9. 6398. 【NOIP2018模拟10.30】Generator(树状数组区间修改)

    题目描述 Description Input Output 输出 q 行,第 i 行表示数据 Di 的答案. Sample Input 4 3 2 1 1 2 4 2 1 2 1 1 3 5 2 2 ...

随机推荐

  1. 安装Genymotion模拟器(第三方)

    优势: 启动速度更快 注册账户,下载可用的系统镜像,就可以使用.     官方网站: https://www.genymotion.com/account/login/ 选择的版本是带VirtualB ...

  2. django Error: HINT: Add or change a related_name argument to the definition for 'UserProfile.groups' or 'User.groups'.

    # 解决方案: 因自己重新封装user为UserProfile故在 settings中 添加自己的

  3. pyharm无法安装包的问题

    1.换成下面这个网址 https://github.com/pypa/pip/issues/5236 2.下载最新的pip  3. 然后换回 https://pypi.org/simple/

  4. Mysql开启慢查询命令

    ; 查询: SHOW VARIABLES LIKE '%slow_query_log%'; 结果:

  5. Educational Codeforces Round 70

    目录 Contest Info Solutions A. You Are Given Two Binary Strings... B. You Are Given a Decimal String.. ...

  6. 1069 The Black Hole of Numbers(20 分)

    For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...

  7. centos6下安装docker

    安装docker对内核版本的要求很高,需要内核3.10以上. 一.docker卸载 查看内核版本: 如果不升级内核到3.10安装docker,后面会有很多奇怪的问题,像我就是拉取不到镜像. 以下我是r ...

  8. FLUENT多相流问题后处理中如何显示其中一相的分布【转载】

    转载自:http://blog.sina.com.cn/s/blog_6a5314cf0100tnsz.html 多相流问题后处理中很容易显示相界面在某些面上的形态,如图1所示.其实利用ISO-Cli ...

  9. CSS 交集选择器和并集选择器

    交集选择器是and 也就是要同时满足 且只能交2个只能交2个只能交2个,第一个是标记,第二个是class或者id,之间不可以有空格 eg:  span.small-height 并集选择器是or,也就 ...

  10. 深入理解JVM虚拟机9:JVM监控工具与诊断实践

    转自https://juejin.im/post/59e6c1f26fb9a0451c397a8c jvm优化必知系列——监控工具 微信公众号[Java技术江湖]一位阿里 Java 工程师的技术小站. ...