pat1057 stack
超时算法,利用2的特殊性,用2个multiset来维护。单个multiset维护没法立即找到中位数。
其实也可以只用1个multiset,用一个中位指针,++,--来维护中位数。
#include<iostream> #include<stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<queue> #include<vector> #include<string> #include<map> #include<stack> #include<set> using namespace std; typedef long long ll; const int maxn = 1e4 + 7; int n; multiset<int> up, lo; stack<int> sta; void adapt() {int cnt = up.size() + lo.size();int upsz = ceil(cnt / 2.0);while (up.size() < upsz) {up.insert(*lo.begin());lo.erase(lo.begin());}while (up.size() > upsz) {int x = *up.rbegin();lo.insert(x);up.erase(up.find(x));} } void push(int x) {up.insert(x);adapt(); } int peek() {return *(up.rbegin()); } void pop(int x) {if (up.find(x) != up.end())up.erase(up.find(x));elselo.erase(lo.find(x));adapt(); } int main() {freopen("in.txt", "r", stdin);cin >> n;while (sta.empty() == false)sta.pop();up.clear(), lo.clear();char cmd[13];int x;while (n--) {cin >> cmd;if (cmd[1] != 'u') {if (lo.empty() && up.empty()) {puts("Invalid");continue;}if (cmd[1] == 'o') {int x = sta.top();sta.pop();cout << x << endl;pop(x);} else if (cmd[1] == 'e') {cout << peek() << endl;}} else {cin >> x;sta.push(x);push(x);}}return 0; }
此题正解树状数组
#include<stdio.h> #include<cstring> #include<iostream> #include<string> using namespace std; const int N=100005; int c[N]; int lowbit(int i){ return i&(-i); } void add(int pos,int value){ while(pos<N){ c[pos]+=value; pos+=lowbit(pos); } } int sum(int pos){ int res=0; while(pos>0){ res+=c[pos]; pos-=lowbit(pos); } return res; } int find(int value){ int l=0,r=N-1,median,res; while(l<r-1){ if((l+r)%2==0) median=(l+r)/2; else median=(l+r-1)/2; res=sum(median); if(res<value) l=median; else r=median; } return l+1; } int main(){ //freopen("D://test.txt","r",stdin); char ss[20]; int stack[N],top=0,n,pos; memset(c,0,sizeof(c)); scanf("%d",&n); while(n--){ scanf("%s",ss); if(ss[1]=='u'){ scanf("%d",&pos); stack[++top]=pos; add(pos,1); }else if(ss[1]=='o'){ if(top==0){ printf("Invalid\n"); continue; } int out=stack[top]; add(out,-1); printf("%d\n",stack[top--]); }else if(ss[1]=='e'){ if(top==0){ printf("Invalid\n"); continue; } int res; if(top%2==0) res=find(top/2); else res=find((top+1)/2); printf("%d\n",res); }else{ printf("Invalid\n"); } } return 0; }
类似题目:zoj3612
转载于:https://www.cnblogs.com/weiyinfu/p/5252624.html
pat1057 stack相关推荐
- pat-1057 Stack 树状数组+二分查找
题意 给我们一个n表示操作数量 然后三种操作 push和pop 还有求中位数的操作 让我们根据操作输出正确的解 分析 用sort排序做 或者 map标记法都会超时 考虑更快的方法 如何快速找到给定一串 ...
- Docker入门六部曲——Stack
原文链接:http://www.dubby.cn/detail.html?id=8739 准备知识 安装Docker(版本最低1.13). 阅读完Docker入门六部曲--Swarm,并且完成其中介绍 ...
- 堆栈,数据,文本,heap,bss,text data,stack
堆栈,数据,文本,heap,bss,text data,stack text data bss stack heap 段 根据APUE,程序分为下面的段:.text, data (initialize ...
- 深度学习加速器堆栈Deep Learning Accelerator Stack
深度学习加速器堆栈Deep Learning Accelerator Stack 通用张量加速器(VTA)是一种开放的.通用的.可定制的深度学习加速器,具有完整的基于TVM的编译器堆栈.设计了VTA来 ...
- 2021年大数据ELK(一):集中式日志协议栈Elastic Stack简介
全网最详细的大数据ELK文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 系列历史文章 一.简介 二.ELK 协议栈介绍及体系结构 三.集中式日志协议栈 ...
- C++实现stack【栈】
要求: //****file: stack.h /* 对stack进行初始化 检查stack为空,或已满 将整数压入到stack中 从stack里弹出整数 不移除任何袁术,讲过stack的内容输出到标 ...
- pytorch学习——torch.cat和torch.stack的区别
合并tensors torch.cat 沿着特定维数连接一系列张量. torch.stack 沿新维度连接一系列张量. torch.cat 在给定维度中连接给定的 seq 个张量序列. 所有张量必须具 ...
- C++ Stack Queue priority_queue
栈stack:stack 后入先出(LIFO) q.top() 获取栈顶元素(并不删除) q.pop() 删除栈顶元素 q.push(x) 向栈中加入元素 q.empty() 判断栈是否为空 队列qu ...
- C++ STL: 超详细 容器 deque 以及 适配器queue 和 stack 源码分析
文章目录 前言 deque 实现 deque类 _Deque_iterator 类 deque 的元素插入 insert函数 deque如何模拟空间连续 queue 实现 stack 的实现 前言 C ...
最新文章
- Spring Boot 中使用@Async实现异步调用,加速任务执行!
- poj 3481 平衡树
- python接口自动化-参数化
- 记录Hibernate的缓存知识
- JDBC事务--软件开发三层架构--ThreadLocal
- 考研961数据结构c语言版真题,严蔚敏数据结构C语言版考研真题库
- NFC:Arduino、Android与PhoneGap近场通信
- 全国大学生计算机创新创意大赛,全国大学生先进成图技术与产品信息建模创新大赛...
- linux ubantu最新版本,过去十年最佳的Ubuntu版本
- html网页有内容不能向下拉,为什么百度页面不能往下拉
- 软件著作权的鉴定材料提交
- 艾美捷测序级 II,纯化胰蛋白酶化验程序文献参考
- 电脑显示计算机资源不足 新用户无法登录,三招解决win10电脑提示资源不足的问题...
- 转载:“只要3分钟,我就能扒光你的隐私!” | 互联网时代,14亿中国人都在裸奔|你的隐私已不是隐私
- 监控广告变现效果,开发者该如何搭建数据分析体系,如何制定优化策略?
- Kibana--KQL和Lucene的区别
- charles证书过期如何处理
- 基于复杂网络的大群体应急决策专家意见与信任信息融合方法及应用
- win10 Telnet服务器 解决telnet正在连接127.0.0.1...无法打开到主机的连接 在端口 23: 连接失败
- 找不到com.mchange.v2.c3p0.ComboPooledDataSource
热门文章
- vs2017 + miniUI 后端框架使用
- 阿里云服务器ip:端口号无法访问
- LA_4670_Dominating_Patterns_(AC自动机+map)
- servlet和struts2一起使用,实现绝对路径下的图片输出到jsp页面
- 寄存器和立即数和内存单元
- .Net给图片添加水印效果
- 学习ios,看到下面的博客感觉收获很大
- [转]2020年4月github上最热门项目-java
- 强烈推荐!入门大数据分析必看的知识点总结,适合零基础学习
- 电力企业计量生产需求系统解决方案