PAT 甲级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 分)(不会,树状数组+二分)*****的更多相关文章
- 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
PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...
- PAT甲级1057 Stack【树状数组】【二分】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...
- PAT 甲级 1057 Stack
https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 Stack is one of the mo ...
- 1057 Stack (30分)(树状数组+二分)
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
- PAT-1057 Stack (树状数组 + 二分查找)
1057. Stack Stack is one of the most fundamental data structures, which is based on the principle of ...
- PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)
1147 Heaps (30 分) In computer science, a heap is a specialized tree-based data structure that sati ...
- 【PAT甲级】1057 Stack (30 分)(分块)
题意: 输入一个正整数N(<=1e5),接着输入N行字符串,模拟栈的操作,非入栈操作时输出中位数.(总数为偶数时输入偏小的) trick: 分块操作节约时间 AAAAAccepted code: ...
- 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 ...
随机推荐
- Java锁--CountDownLatch
转载请注明出处:http://www.cnblogs.com/skywang12345/p/3533887.html CountDownLatch简介 CountDownLatch是一个同步辅助类,在 ...
- vue 单向数据流
- Python 装饰器实现单列模式
# 使用装饰器实现单列模式 def singleton(cls): # 用来存在实例的字典 singleton_instance = {} def wrapper(*args, **kwargs): ...
- 01_Request和Response
参考文档 http://www.iamnancy.top/djangorestframework/Responses/ https://q1mi.github.io/Django-REST-frame ...
- HDU 6170 - Two strings | 2017 ZJUT Multi-University Training 9
/* HDU 6170 - Two strings [ DP ] | 2017 ZJUT Multi-University Training 9 题意: 定义*可以匹配任意长度,.可以匹配任意字符,问 ...
- 使用PS进行切图
一,设置PS 使用PS进行切图前的设置: 1,打开PS----打开PSD图片----点击窗口-----分别把:历史记录,信息,图层,三个打勾. 历史记录:可以回到之前想要的步骤,特别是不小心把图层的文 ...
- 百度静态资源库CDN库, cdnjs库,引入JS
不适用本地引入js文件,而使用其他服务器引入JS文件,1,减轻服务器压力2,速度快3,可以缓存 cdnjs库,更新比较快https://cdnjs.com/ cdn库 引入JS文件如:jquerybo ...
- mysql查询重复数据
SELECT * FROM oa_user ) ORDER BY UserName oa_user表名,UserName需要查重复的字段名
- Qt--解析Json
一.QT5 Json简介 QT4中使用第三方库QJson解析JSON文件. QT5新增加了处理JSON的类,类均以QJson开头,包含在QtCore模块中.QT5新增加六个相关类: QJsonArra ...
- 服务器之poll
poll服务器方法采用将监听端口用数组存放起来,这样就不需要轮询的监听整个文件描述符了 #include <poll.h> int poll(struct pollfd *fds, nfd ...