PAT 甲级 1057 Stack

https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592

 

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

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;
int N;
int c[maxn];
stack<int> s;

int lowerbit(int x) {
    return x & -x;
}

void update(int x, int v) {
    for(int i = x; i < maxn; i += lowerbit(i))
        c[i] += v;
}

int getsum(int x) {
    int sum = 0;
    for(int i = x; i >= 1; i -= lowerbit(i))
        sum += c[i];
    return sum;
}

void PeekMedian() {
    int left = 1, right = maxn, mid, k = (s.size() + 1) / 2;
    while(left < right) {
        mid = (left + right) / 2;
        if(getsum(mid) >= k)
            right = mid;
        else left = mid + 1;
    }
    printf("%d\n", left);
}

int main() {
    scanf("%d", &N);
    while(N --) {
        string op;
        cin >> op;
        if(op == "Pop") {
            if(s.empty()) printf("Invalid\n");
            else {
                printf("%d\n", s.top());
                update(s.top(), -1);
                s.pop();
            }
        } else if(op == "Push") {
            int num;
            scanf("%d", &num);
            s.push(num);
            update(num, 1);
        } else {
            if(s.empty()) printf("Invalid\n");
            else PeekMedian();
        }
    }
    return 0;
}

  树状数组 第一次写到树状数组 看博客还没看的很懂但是隐约感觉到这是一个模板题 emmmm 以后多做一点就会明白了叭 唉 哭唧唧

FH

转载于:https://www.cnblogs.com/zlrrrr/p/10357628.html

5分钟搞定DataDome滑块验证码:最新Capsolver API实战教程(含代理配置) 本文提供了一份利用Capsolver API快速自动化破解DataDome滑块验证码的实战教程。文章深入剖析了DataDome验证码的环境指纹与行为分析机制,并重点详解了确保成功率的住宅代理配置、API集成步骤及令牌使用等核心环节,帮助开发者高效集成稳定可靠的自动化解决方案。 阅读详情

相关推荐

同花顺【红绿趋势】幅图指标 源码分析

a10:IF(差值<=0 ,0 ,差值*0.2 ,4 ,0),STICK,linethick5,colorred;a7:IF(差值<=0 ,0 ,差值*0.8 ,4 ,0),STICK,linethick5,colorred;a8:IF(差值<=0 ,0 ,差值*0.6 ,4 ,0),STICK,linethick5,colorred;a6:IF(差值<=0 ,0 ,差值*1 ,4 , 0),STICK,linethick5,colorred;DRAWICON(先锋<0,-25,1);

zhibiaogong的博客 4094

PAT A 甲级 1057 Stack (30分)

1057 Stack (30分) 吐槽 ……这题让我找回了刚开始练pat、STL都还不会用的阶段——总是报各种各样的错误而完全没有修改的头绪。 思路 先提一嘴scanf的事。这道题其实用cin时间也是够的,只不过会从50ms左右变成200+。总之不是必需。 然后是主干。 这道题非常容易超时。非常。 如果真的用vector保存数字,然后每次找中位数时都真的去找一下,又或者用两种sort方式分别处理p...

虚境的博客 312

保姆级教程:在阿里云ECS上用Docker一键部署EMQX MQTT服务器(Ubuntu 20.04)

本文提供在阿里云ECS上使用Docker一键部署EMQX MQTT服务器的详细教程,涵盖从实例创建、Docker环境配置到EMQX部署与优化的全流程。适合物联网开发者快速搭建高效、安全的MQTT消息服务器,适用于智能家居、工业物联网等场景。

weixin_28739079的博客 319

PAT甲级-1057 Stack (30分)【转载/树状数组】

点击链接PAT甲级-AC全解汇总 原文转自: 李小白~:1057 Stack (30分) 李小白~:树状数组(简单介绍) 其他: 柳婼:【数据结构】树状数组笔记 柳婼:1057. Stack (30)-PAT甲级真题(树状数组) Uncle_Sugar:1057. Stack (30)五种解法总结(大杂烩) 非科班出身,这太难了,头秃 我的代码: #include<bits/stdc++.h...

卷子的博客 446

Oracle监听配置

Oracle监听配置## 标题 选择“监听程序配置”,然后下一步。 选择重新配置,然后下一步/。 不用管,按下一步。 下一步。 选择“否”,下一步。 选择"本地net服务名配置",然后下一步。 选择“重新配置”,然后下一步。 服务名 是"Oracle",然后下一步。 主机为你的IP地址,在doc系统下通过"ipconfig"获取IP地址。然后输入,然后下一步。 选择“是,进行测试...

m0_46079215的博客 808

[技术解析]4DGS:突破实时动态渲染的4D高斯溅射技术

本文深入解析了4DGS(4D Gaussian Splatting)技术,它通过引入时间维度,将静态的3D高斯溅射升级为动态的4D表示,实现了对复杂动态场景的高质量实时渲染。该技术利用时空联合编码器与高斯形变场网络,结合多分辨率编码与差分溅射渲染,在VR/AR、高帧率游戏等领域展现出突破性的应用潜力,兼顾了渲染速度与画面稳定性。

weixin_29218509的博客 443

PAT 甲级 1057  Stack

1057 Stack (30 point(s)) 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 ont...

漫浸天空的雨色的备忘录~ 745

CoaXPress协议入门(1)---- CXP测试套件

CoaXPress (简称CXP)是指一种采用同轴线缆进行互联的相机数据传输标准,主要用于替代之前的Camera Link协议,常见于科学相机、工业相机、医学图像、航空防务等场景。CXP是一个非对称的高速点对点串行传输协议,主要用于传输视频和静态图像,线缆多使用单条或多条同轴电缆。CXP易于集成,可以获得灵活可靠的性能,通过使用标准的同轴线缆,比如RG59 和 RG6规格(推荐使用RG6),并且图像、控制通讯、触发、电源都可以使用同一条线缆。

素手挽剑 9545

【PAT甲级 - C++题解】1057 Stack

📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!

Newin2020的博客 535

OPC通信从入门到精通_1_OPC基础知识及简单C#程序编写(OPCDA,OPCUA简介;OPC通信数据流框架图;C#程序编写)

ModbusTCP通信是采用直接方式进行通信,用OPC的方式如何去做呢?PLC作为ModbusTCP服务器,用KepServer作为ModbusTCP客户端,同时作为OPC服务器,PC作为OPC客户端新建项目,加载dll库(利用COM技术写的)添加引用,查看到其中包含了众多方法其中OPCGroups继承于IOPCGroups接口。

分享学习成长历程,Winter is coming! 1万+

遇到问题--OpenClaw--API rate limit reached. Please try again later429 您的账户已达到速率限制,请您控制请求频率

OpenClaw调用glm-4.7-flash模型时触发智谱AI平台429速率限制错误。分析发现当前重试策略采用固定间隔(7-11秒)导致连续失败,且未记录关键响应头信息。解决方案分三步:1)开启响应头日志获取配额详情;2)将重试策略改为带抖动的指数退避(初始1秒,最大60秒);3)在网关层实现令牌桶限流主动控制请求频率。需特别注意令牌配额(TPM)可能比请求次数(RPM)更早触达限制。

直到世界的尽头 2650

PAT甲级 1057 Stack

浙江大学PAT题目解答内容. Contribute to ZouJiu1/PAT development by creating an account on GitHub.GitHub - ZouJiu1/PAT: 浙江大学PAT题目解答内容。block,计数的时候使用块计算总和,不要单个相加。

邹九的个人博客 195

ICC2:postmask ECO参考脚本

更多学习内容请关注「拾陆楼」知识星球。

m0_61544122的博客 887

PAT 甲级真题 1057 Stack

题目链接 题解: 题目就是求第k小元素问题。由于操作次数比较多,因此直接排序输出中间元素会超时,因此这题要用到第K小元素的经典做法之一,树状数组。用树状数组来维护现在栈内元素的信息,然后使用二分查找中间元素的值。这样时间复杂度是O(nlogn)O(nlogn)O(nlogn)。 实现细节见代码: #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int c[MAXN]; stack<int&g

weixin_43989731的博客 229

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

PAT甲级1057 Stack

本来想用做的,重复的值保存其个数在对应的数组,但是逻辑还是太麻烦了; 最后用的是算法笔记中的代码。 #include<bits/stdc++.h> using namespace std; stack<int> s; int block[317]={0},num[100001]={0}; void Median(int k) { int cn=0,cb=0; while(cn+block[cb]<k) { cn+=block[cb++]; } int change

m0_46515304的博客 170

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

pat 甲级 1057 Stack

用sort只过得15分 大佬们都用了树状数组 没用树状数组的思路看懂了但是那个语法不会写 树状数组 个人理解:用lowbit把a化成二进制,查零,有k个零的时候,a+k为父亲数组,a-k为上一个是父亲数组的数组 不过题解的代码里没看明白这个是怎么用的。。。迷茫 超时代码↓ #include &amp;amp;lt;cstdio&amp;amp;gt; #include &amp;amp;lt;iostream&amp;amp;gt; #inc...

咸鱼之井的博客 345

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

PAT(甲级)1057 Stack(树状数组)

Description 题目大意:在Stack的原有功能上增加了PeekMedian功能,即排好序后求中位数 Input Push key Pop PeekMedian Output 相应操作的值 解题思路 算法标签:树状数组 首先分析为什么用树状数组。显然对于pop和push两种操作无需考虑,在求中位数时候,为了得到中位数,显然需要排序好的序列才能得到,如果每次都得排序,显然时间复杂度会很高,如果是快排的话(O(nlgn)),最坏情况可能为(O(n2lgn))。所以关键问题在于查询。假设tree[n]记录

TSWorld 304

PAT甲级 1057 Stack (超简单写法)

题目链接 题目大意 模拟一个栈的push和pop操作,且在输入PeekMedian时输出栈里所有数的中位数。 解题思路 使用c++的内置库stack和pb_ds可快速解决,无需手写高级数据结构或者手动二分(懒)。使用了自带的红黑树中的**find_by_order(k)**查找第k大的元素,实现中位数查找,由于此处不支持mulity-set,所以不能有重复元素,需要用map定义一个偏移量,具体看代...

zhixin169的博客 319

PAT真题练习(甲级)1057 Stack (30 分) (使用multiset和stack实现)

PAT真题练习(甲级)1057 Stack (30 分) 原题网址:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 Stack is one of the most fundamental data structures, which is based on the principle ...

Be Quick & Clever & Lucky 632

PAT甲级——A1057 Stack

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) a...

weixin_30632089的博客 128

PAT甲级1057 Stack(JAVA版)

本题考查 树状数组 树状数组解析 树状数组(Binary Indexed Tree(B.I.T), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据结构,其原理: A为正常数组,C为树状数组,A与C的元素有以下关系: C1 = A1 C2 = A1 + A2 C3 = A3 C4 = A1 + A2 + A3 + A4 C5 = A5 C6 = A5 + A6 C7 = A7...

R.I.P Kobe Bryant 322

PAT甲级 1057 Stack (30 分)

一、暴力解法(22分) \quad题目告诉我们数字取值范围为[0,100000],因此我们用数组c[100010]来表示某个数字出现次数,初始化为0。比如2被push进栈中,就令a[2]++,3被pop出去,则a[3]–。这样求n个数的中位数就变成了求c数组中前i个数的和sum使得sum≥n/2时的值即为中位数。 #include <bits/stdc++.h> using names...

qq_40438165的博客 1725

pat 甲级 分块 A1057 Stack (30分)

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

weixin_42358356的博客 158

PAT甲级(1057):Stack

树状数组参考链接很详细:树状数组详解 #include <iostream> #include <stack> #define lowbit(i) ((i) & (-i)) const int maxn = 100010; using namespace std; int c[maxn]; stack<int> s; void update(int x, int v) { for(int i = x; i < maxn; i += lowbit(i)

weixin_42701161的博客 231

1057 Stack (30 分)-PAT甲级

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 im

神笔码农.的博客 462

PAT甲级1057stack---对顶堆

前言 这题我搜了一下,好像没有人用对顶堆做,都是树状数组。事实上我第一次接触动态中位数就是用对顶堆做的,我就不服,我就要楞头用对顶堆做,但是这题我忙活了半天踩了好多坑。。。终于被我用堆顶堆搞出来的。 题目 1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the pri...

qq_44196094的博客 225
上一篇: 【学习总结】GirlsInAI ML-diary day-7-数据类型转换
下一篇: Codeforces1106F 【BSGS】【矩阵快速幂】【exgcd】
weixin_30622107
博客等级 码龄11年 117粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值