pat a1057 栈【分块思想】

嵌入式Linux开发必备:设备树插件动态加载实战指南(RK356x平台为例) 本文以RK356x平台为例,详细介绍了嵌入式Linux开发中设备树插件的动态加载实战。通过configfs接口,开发者可以在系统运行时动态修改硬件配置,实现外设的即插即用与调试,无需重启设备,极大提升了开发与调试效率。 阅读详情

题目:

https://www.nowcoder.com/pat/5/submission-detail/64563013

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 (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 (≤10​5​​). 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 10​5​​.

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

 给出一个栈的入栈(Push)、出栈(Pop)过程,并随时通过PeekMedian命令要求输出栈中中间大小的数(Pop命令输出出栈的数)。当栈中没有元素时,Pop命令和PeekMedian命令都应该输出Invalid。

二、算法实现 

1、

#include<cstdio> 
#include<cstring>
#include<stack>
using namespace std;

const int maxn=100010;
const int sqrN=316;

stack<int> st;
int block[sqrN];
int table[maxn];

void peekMedian(int K)
{
	int sum=0;
	int idx=0;  //块号 
	while(sum+block[idx]<K)
	{
		sum+=block[idx++];
	}
	int num=idx*sqrN;
	while(sum+table[num]<K)
	{
		sum+=table[num++];
	} 
	printf("%d\n",num);
}

void Push(int x)
{
	st.push(x);
	block[x/sqrN]++;
	table[x]++;
}

void Pop()
{
	int x=st.top();
	st.pop();
	block[x/sqrN]--;
	table[x]--;
	printf("%d\n",x);	
}

int main()
{
	int x,query;
	memset(block,0,sizeof(block));
	memset(table,0,sizeof(table));
	char cmd[20];
	scanf("%d",&query);
	
	for(int i=0;i<query;i++)
	{
		scanf("%s",cmd);
		if(strcmp(cmd,"Push")==0)
		{
			scanf("%d",&x);
			Push(x);
		}
		else if(strcmp(cmd,"Pop")==0)
		{
			if(st.empty()==true)
			{
				printf("Invalid\n");
			}
			else
			{
				Pop();
			}
		}
		else
		{
			if(st.empty()==true)
			{
				printf("Invalid\n");
			}
			else
			{
				int K=st.size();
				if(K%2==1) K=(K+1)/2;
				else K=K/2;
				peekMedian(K);
			}
		}		
	}
	return 0;
}

2、

#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
const int MAX = 100001;
const int MAXN = 317;
int n, block[MAXN] = {0}, table[MAX] = {0};
char order[11];
stack<int> s;
 
bool cmp(int a, int b)
{
	return a < b;
}
 
int main()
{
	scanf("%d", &n);
	while(n--)
	{
		scanf("%s", order);
		if(strcmp(order, "Pop") == 0)
		{
			if(!s.empty())
			{
				int k = s.top();
				printf("%d\n", k);
				s.pop();
				block[k/(MAXN-1)]--;
				table[k]--;
			}
			else printf("Invalid\n");
		}
		else if(strcmp(order, "PeekMedian") == 0)
		{
			if(s.empty())
			{
				printf("Invalid\n");
				continue;
			}
			int k, idx = 0, index = 0;
			if(s.size() % 2 == 0) k = s.size()/2;
			else k = (s.size()+1)/2;
			int sum = 0;			
			while(sum + block[idx] < k)
			{
				sum += block[idx++];
			}
			index = idx * (MAXN-1);
			while(sum + table[index] < k)
			{
				sum += table[index++];
			}
			printf("%d\n", index);
		}
		else
		{
			int k;
			scanf("%d", &k); 
			s.push(k);
			table[k]++;
			block[k/(MAXN-1)]++;
		}
	}
	return 0;
}

 

Cadence OrCAD Capture软件启动过程卡死且无法恢复的解决方法 如下图所示Cadence OrCAD Capture软件在启动后出现卡死的现象,且等待后无法自动恢复,需要强行关闭,再次启动依然卡死。 阅读详情

相关推荐

有限状态机FSM详解(一)

有限状态机在维基百科中的解释是: 有限状态机(英语:finite-state machine,缩写:FSM)又称有限状态自动机,简称状态机,是表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型。 至于为什么要学FSM,FSM可以把模型的多状态、多状态间的转换条件解耦。可以使维护变得容易,代码也更加具有可读性,也更加艺术。 理解有限状态机首先需要理解整个模型。我们把灯的开关作为示例,...

benjonc的博客 5万+

PAT A 甲级 1057 Stack (30分)

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

虚境的博客 312

鱼眼相机去畸变(图像拉直/展开/矫正)算法及实战总结

1、算法说明经纬度矫正法, 可以把鱼眼图想象成半个地球, 然后将地球展开成地图,经纬度矫正法主要是利用几何原理, 对图像进行展开矫正。经过P点的入射光线没有透镜的话,本应交于相机成像平面的e点。然而,经过鱼眼相机的折射,光线会交于相机成像平面的d点,就产生了畸变,因此畸变图像整体上呈现出像素朝图像中心点聚集的态势。而去畸变,就是将折射到d点的点,重新映射回到e点,因此去畸变之后的图像与原始的鱼眼图像相比,仿佛是把向心聚集的像素又重新向四周铺展开来。AVM环视系统——鱼眼相机去畸变算法 - 知乎。

大胡子大叔的专栏 2万+

PAT甲级1057

1057. Stack (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Stack is one of the most fundamental data structures

湛蓝的天空 1179

从稀疏表示到低秩表示(一)

确定研究方向后一直在狂补理论,最近看了一些文章,有了些想法,顺便也总结了representation系列的文章,由于我刚接触,可能会有些不足,愿大家共同指正。 从稀疏表示到低秩表示系列文章包括如下内容: 一、 sparse representation 二、 一、 sparse representation 1  sparsity  一个线性表示解决的问题如下图所示:

彼岸花 3万+

Pywinauto与Inspect.exe完美搭配:解决Windows应用UI自动化中的元素定位难题

本文详细介绍了如何将Pywinauto与Inspect.exe结合,以解决Windows应用UI自动化中的核心难题——元素定位。通过实战指南,阐述了如何利用Inspect.exe在UIA模式下精准侦察控件属性,并将这些信息转化为Pywinauto的稳健定位策略,从而高效自动化WPF、WinForms等现代框架构建的复杂桌面应用。

weixin_29297127的博客 520

PAT A1057

方法:运用分块与哈希的思想,用block和hashtable分别记录每块与每个数的数据个数。 #include #include using namespace std; int hashtable[100010]={0},block[320]={0}; int main(){ int N,temp; stack s; char str[20]; scanf("%d",&N); w

weixin_37565706的博客 328

STK传感器约束设置保姆级教程:从方位角到传播延迟,手把手教你避开新手常见坑

本文详细解析了STK传感器约束设置的关键技巧,涵盖方位角、仰角、高度及传播延迟等核心参数。通过实战案例和代码示例,指导用户避开常见误区,优化卫星通信和航天任务仿真精度,特别适合STK新手和需要提升传感器配置效率的工程师。

weixin_33701294的博客 393

PAT——A1057 Stack(分块思想 第K大)

题目链接: #include&lt;cstdio&gt; #include&lt;iostream&gt; #include&lt;map&gt; #include&lt;set&gt; #include&lt;cstring&gt; #include&lt;algorithm&gt; #include&lt;cmath&gt; #include&lt;vector&gt; #

ZMST的博客 361

ThingsBoard 仪表板状态

请先通过阅读仪表板概述和仪表板入门案例来熟悉ThingsBoard的仪表板。文章希望通过浅显的文字描述什么是仪表板状态及如何使用。 概念 状态功能使得仪表板可以分层,配合小部件的动作,实现多层仪表板之间的跳转.每个状态对应一个仪表板的图层 每个仪表板有一个默认的根状态, 根状态可以理解为默认状态, 也就是打开仪表板默认展示的仪表板图层.编辑模式下, 点击右上角的图标来管理状态.添加多个状态后左上角出现状态切换(图层切换)下拉列表可以通过选中根状态复选框切换新的默认展示的状态/图层配合小部件的动作,实现状

物联网三分钟 2308

基于XGBoost的混凝土力学性能预测系统

本文介绍了一个基于XGBoost算法的混凝土力学性能预测系统。该系统通过模拟生成3000组混凝土配合比数据(包含水灰比、水泥用量和龄期三个关键特征),利用XGBoost强大的非线性建模能力预测混凝土抗压强度。研究背景源于传统混凝土性能测试方法耗时耗力的痛点,该系统可显著减少实验次数,优化配合比设计。系统实现包括:数据生成与特征工程(模拟真实工程数据并添加噪声和异常值)、XGBoost模型构建、以及预测性能评估。XGBoost算法因其处理非线性关系能力强、鲁棒性好等优势被选用,可为智能建造和数字化施工提供技术

huanghm88的专栏 131

PAT 1057. Stack

题目:http://pat.zju.edu.cn/contests/pat-a-practise/1057 题解: 维护一个小于等于mid的,一个大于mid的两部分,保持小的的大小和大的的大小一致或者比大的多一个,这样mid即小的的最后一个数。 代码: #include #include #include #include #include #include #include #inclu

Ted's 游乐园 2908

化工设计软件:PDMS二次开发_(7).数据导入与导出技术

在某些复杂场景下,PDMS提供的标准API接口可能无法满足需求。此时,可以通过自定义API接口来实现特定的数据导入与导出功能。自定义设备导入API接口可以处理更复杂的设备数据格式。# 导入必要的库import pdms_api # 假设有一个PDMS API库# 定义自定义设备数据# 自定义设备导入API接口# 调用自定义设备导入API接口# 输出导入成功的消息print("自定义设备数据导入成功")在PDMS二次开发中,数据的导入与导出是关键的技术环节。

kkchenjj的博客 1487

PAT (Advanced Level) Practice A1057 Stack (30 分)(C++)(甲级)(栈、分块思想)

原题链接:A1057 Stack #include&amp;lt;algorithm&amp;gt; #include&amp;lt;iostream&amp;gt; #include&amp;lt;cstdio&amp;gt; #include&amp;lt;cstring&amp;gt; using namespace std; const int MAX = 100010, SQRT = 316; int N, key,

izhxxx的博客 178

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

PAT甲级 A1057

PAT甲级 A1057 题目详情 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 el

weixin_44065549的博客 288

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

PAT 1057 Stack C++版

PAT 1057 Stack C++版 1.题意 给出一系列栈的操作,让我们模拟动态查询(即在线查询)。 本体的在线查询主要是找出中位数,即一个数组中中间的那个数。关于一个数组中中间数的定义如下: With N elements, the median value is defined to be the (N/2)-th smallest element if N is even, or ((...

说文科技,做有态度的研究。 569

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

PAT A1057 分块思想

使用的就是分块思想,之前写过总结,所以不再赘述;代码如下: #include&lt;iostream&gt; #include&lt;stdlib.h&gt; #include&lt;stdio.h&gt; #include&lt;stack&gt; #include&lt;cstring&gt; using namespace std;...

weixin_34126215的博客 94

STM32从入门到精通:内核架构、外设编程与避坑指南

在嵌入式开发中,32位微控制器(MCU)是连接物理世界与数字系统的核心。基于ARM Cortex-M内核的STM32系列,凭借其丰富的外设、成熟的生态和灵活的选型,成为工业控制、物联网和智能硬件的首选平台。理解其时钟树、中断管理、存储器映射及外设编程模型,是掌握嵌入式开发的关键。从GPIO、定时器到ADC、DMA,再到FreeRTOS与物联网网关应用,STM32覆盖了从基础控制到复杂系统的广泛场景。本文深入解析STM32的家族选型、内核原理、开发环境搭建及常见问题排查,帮助开发者系统掌握这一主流MCU,提升

weixin_34232744的博客 278

PAT 1057 分块思想

题目 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...

qq_44910084的博客 199

【PAT】A1057 Stack (30分)(分块思想 / 树状数组)

1057 Stack (30分) 题目链接 思路:分块的思想,详细略。 #include<iostream> #include<cmath> #include<algorithm> #include<stack> #include<cstring> using namespace std; const int maxn = 10000...

醉酒诗人的博客 304

【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) and Po...

elegant coder 759

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

STM32交期压缩至24小时:SMT产线供应链重构实践

STM32作为嵌入式开发最主流的MCU,其供应稳定性长期制约SMT产线交付效率。传统分销模式下,芯片从原厂到贴片机平均耗时超40天,根源在于多级代理、静态库存模型与ERP系统割裂。本文基于动态安全库存算法(DSIA)与ERP直连API等工程实践,解析如何将STM32交期压缩至24小时内——核心并非堆库存,而是以产线实时负荷、BOM替代弹性、物流响应能力为变量重构库存逻辑,并通过轻量级中间件打通ERP‘数据血管’。该方案已验证可提升库存周转率4.1倍、紧急订单满足率达99.2%,使工艺工程师从兼容适配转向US

weixin_33797791的博客 277

PAT练习笔记——13.1 分块思想

2019年9月PAT - 练习笔记——13.1 以下页码标注的是阅读器中实际页码,而不是书本身自印的页码。 第13章 提高篇(6)——专题扩展 13.1 分块思想 目录 A1057 Stack A1057 Stack Stack is one of the most fundamental data structures, which is based on the principle ...

qreamly的博客 257

PAT-ADVANCED1057——Stack

我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED 原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题目描述: 题目翻译: 1057 栈 栈是最基本的数据结构之一,它基于后进先出(LIFO)原则。基本...

清风阁 191
上一篇: 待续....
下一篇: K-Means Clustering 笔记
Estrellas_
博客等级 码龄9年 273粉丝 233原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值