1057 Stack (PAT甲级)

news/2025/2/13 0:07:28/

用了《算法笔记》中分块的思路。

#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
const int MAXN = 100001;int main(){int N, sz, nbrOfBlock, t, cnt, j;int count[MAXN] = {0};sz = sqrt(MAXN * 1.0);nbrOfBlock = MAXN % sz ? MAXN / sz + 1 : MAXN / sz;std::vector<int> block(nbrOfBlock, 0);std::string str;std::vector<int> vec;std::cin >> N;for(int i = 0; i < N; ++i){std::cin >> str;if(str == "Pop"){if(vec.empty()){printf("Invalid\n");} else{printf("%d\n", vec.back());count[vec.back()]--;block[vec.back() / sz]--;vec.pop_back();}} else if(str == "PeekMedian"){if(vec.empty()){printf("Invalid\n");} else{t = vec.size() % 2 ? (vec.size() + 1) / 2 : vec.size() / 2;cnt = 0;for(j = 0; j < nbrOfBlock; ++j){cnt += block[j];if(cnt >= t){cnt -= block[j];break;}}for(int k = j * sz; k < j * sz + sz; ++k){cnt += count[k];if(cnt >= t){printf("%d\n", k);break;}}}} else{std::cin >> t;++count[t];++block[t / sz];vec.push_back(t);}}return 0;
}

根据树状数组:

#include <cstdio>
#include <iostream>
#include <string>
#include <stack>
#define lowbit(x) ((x) & (-x))
const int MAXN = 100001;int N, t;
int c[MAXN] = {0};
std::string str;
std::stack<int> st;void update(int x, int v){for(int i = x; i < MAXN; i += lowbit(i)){c[i] += v;}
}int getSum(int x){int sum = 0;for(int i = x; i > 0; i -= lowbit(i)){sum += c[i];}return sum;
}void PeekMedian(){int sz, left, right, middle;sz = (st.size() + 1) / 2;left = 1;right = MAXN;while(left < right){middle = (left + right) / 2;if(getSum(middle) < sz){left = middle + 1;} else{right = middle;}}printf("%d\n", left);
}int main(){scanf("%d", &N);for(int i = 0; i < N; ++i){std::cin >> str;if(str == "Pop"){if(st.empty()){printf("Invalid\n");} else{t = st.top();st.pop();printf("%d\n", t);update(t, -1);}} else if(str == "Push"){scanf("%d", &t);st.push(t);update(t, 1);} else{if(st.empty()){printf("Invalid\n");} else{PeekMedian();}}}return 0;
}

题目如下:

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 (≤105). 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 105.

Output Specification:

For each Push command, insert key into the stack and output nothing. For each Pop or PeekMediancommand, print in a line the corresponding returned value. If the command is invalid, print Invalidinstead.

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

http://www.ppmy.cn/news/74768.html

相关文章

OKR是什么意思啊

一、OKR是什么意思&#xff1f; OKR是"Objective and Key Results"的缩写&#xff0c;即目标和关键结果。它是一种目标管理框架&#xff0c;旨在帮助组织和团队设定明确的目标&#xff0c;并通过关键结果来衡量和追踪目标的实现情况。 为了让大家快速了解什么是OKR…

ResourceManager启动报错:Queue configuration missing child queue names for root【已解决】

Queue configuration missing child queue names for root 现象报错分析ResourceManager输出日志解决 现象 start-all.sh后缺少RM的进程 报错 查看启动日志输出文件 2023-05-23 19:28:19,863 INFO [main] resourcemanager.RMNMInfo (RMNMInfo.java:<init>(63)) - Re…

UNIX网络编程卷一 学习笔记 第十五章 Unix域协议

本书中&#xff0c;作者说Unix域数据报套接字是不可靠的&#xff0c;这一说法已经过时&#xff0c;当前大多实现中&#xff0c;Unix域套接字都是可靠的&#xff0c;不论是数据报套接字还是字节流套接字。 Unix域协议不是一个实际的协议族&#xff0c;而是单个主机上执行客户/服…

ROS学习(3)——CMakeLists文件的编写

学会CMakeLists文件编写是学习ros一个很重要的知识&#xff0c;但是因为每个人编写的CMakeLists不同&#xff0c;当初学习的时候我查了很多学习资料发现依旧很难入门&#xff0c;所以现在准备详细全面的介绍一下CMakeLists里面包含哪些内容&#xff0c;如何根据自己的项目编写自…

3D点云数据转为俯瞰图Python实现代码

我主要是参考了英文博客来撰写本篇文章&#xff0c;仅作为个人学习笔记参考使用。 文章目录 一、点云数据二、图像与点云坐标三、创建点云数据的鸟瞰视图3.1 鸟瞰图的相关坐标轴3.2 限制点云数据范围3.3 将点位置映射到像素位置3.4 切换到新的零点3.5 像素值3.6 创建图像矩阵3.…

8、Ray社区和资源

8、Ray社区和资源 导航 1.简介和背景 2.Ray的基本概念和核心组件 3.分布式任务调度和依赖管理 4.对象存储和数据共享 5.Actor模型和并发编程 6.Ray的高级功能和扩展性 7.使用Ray构建分布式应用程序的案例研究 8.Ray社区和资源 9.核心框架介绍 10.扩展1

实时频谱-2.1实时频谱分析仪的工作方式

现代实时频谱分析仪 现代实时频谱分析仪可以采集分析仪输入频率范围内任何地方的传输频带或频宽。这一功能的核心是RF 下变频器&#xff0c;后面跟有一个宽带中间频率(IF)段。ADC数字化IF信号&#xff0c;系统以数字方式执行所有进一步的步骤。DSP算法执行所有信号调节和分析功…

cpp综合项目—机房预约系统

目录 1、机房预约系统需求 1.1、 身份简介 1.2、机房简介 1.3、申请简介 1.4、 系统具体需求 2、创建项目 3、创建主菜单 3.1 菜单实现 3.2、接口实现 4、退出功能实现 4.1、退出功能实现 4.2、测试退出 5、创建身份类 5.1、身份的基类 5.2、学生类 5.3、老师类…