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. 从groupby 理解mapper-reducer

    注,reduce之前已经shuff. mapper.py #!/usr/bin/env python """mapper.py""" imp ...

  2. webpack 配置react脚手架(六):api

    1 访问网址 https://cnodejs.org/api 可以调取api 2.//该body-parser 可以将请求的body数据,转变成 json 格式数据://express-session ...

  3. 网站入侵工具 SQL注入神器

    0x 00 前言 SQLMAP 0x 01 注入原理              不说了 *****************************************结束分割线********** ...

  4. AfxBeginThread深入解析

    看过<深入浅出MFC>书中,j.j.hou提到在创建新的线程时,最好不要直接使用CreateThread,虽然AfxBeginThread也是对CreateThread的封装,但是AfxB ...

  5. flask 框架 转载:https://cloud.tencent.com/developer/article/1465968

    特点总结: 类名称---->数据库表名 类属性---->数据库字段 类的对象----->数据库表中的一行一行数据 3.ORM操作注意(理解) 1/因为SQLALChemy去app身上 ...

  6. go 变量的定义方式

    var a int a = 1 var a,b int a =1 b = 2 var a,b = 1,2 var s string = "hello world" a, b := ...

  7. [cf920E][set+dfs]

    https://codeforc.es/contest/920/problem/E E. Connected Components? time limit per test 2 seconds mem ...

  8. Linux 查看实时网卡流量的几种方式

    在工作中,我们经常需要查看服务器的实时网卡流量.通常,我们会通过这几种方式查看Linux服务器的实时网卡流量. 1. sar -n DEV 1 2 sar命令包含在sysstat工具包中,提供系统的众 ...

  9. hash 算法

    Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是 ...

  10. php利用webuploader实现超大文件分片上传、断点续传

    PHP用超级全局变量数组$_FILES来记录文件上传相关信息的. 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本 ...