Stack (30)(模拟栈,输出中间数用set)
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.
输入描述:
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.
输出描述:
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.
输入例子:
17
Pop
PeekMedian
Push 3
PeekMedian
Push 2
PeekMedian
Push 1
PeekMedian
Pop
Pop
Push 5
Push 4
PeekMedian
Pop
Pop
Pop
Pop
输出例子:
Invalid
Invalid
3
2
2
1
2
4
4
5
3
Invalid
题解:一开始我的想法是用vector来模拟栈,排完序后输出中间的值,但是似乎vector的效率并不高
故超时,感觉思路没什么错误。。。
第一次超时代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
int main()
{
stack<int>s;
vector<int>v;
vector<int>::iterator it;
string str;
int n;
int k;
cin>>n;
for(int t=0;t<n;t++)
{
cin>>str;
if(str=="Pop")
{
if(!s.empty())
{
printf("%d\n",s.top());
it=v.begin()+s.size()-1;
v.erase(it);
s.pop();
}
else
{
printf("Invalid\n");
}
}
else if(str=="PeekMedian")
{
sort(v.begin(), v.end(),less<int>());
if(!s.empty())
{
if(s.size()%2==1)
{
it=v.begin()+(s.size()+1)/2-1;
printf("%d\n",*it);
}
else
{
it=v.begin()+s.size()/2-1;
printf("%d\n",*it);
}
}
else
{
printf("Invalid\n");
}
}
else
{
scanf("%d",&k);
s.push(k);
v.push_back(k);
}
}
return 0;
}
换用set的排序功能可以降低复杂度
AC代码:
#include <iostream>
#include <vector>
#include <set>
#include <stack>
#include <cstring>
using namespace std;
stack<int> stk;
multiset<int> set1,set2;
int mid;
//set1存mid及以下的数,其大小应该与set2相等,或多1
void adjust(){
if(set1.size()<set2.size()){//将set2最小的移到set1
auto it=set2.begin();
set1.insert(*it);
set2.erase(it);
}else if(set1.size()>set2.size()+1){//将set1最大的移到set2
auto it=set1.end();
it--;
set2.insert(*it);
set1.erase(it);
}
//重新计算mid
if(!stk.empty()){
auto it=set1.end();
it--;
mid=*it;
}
}
int main(){
int N;
scanf("%d",&N);
char ch[12];
int top,x;
while(N--){
scanf("%s",ch);
if(strcmp(ch,"Pop")==0){
if(stk.empty())
printf("Invalid\n");
else{
top=stk.top();
stk.pop();
printf("%d\n",top);
if(top<=mid)
set1.erase(set1.find(top));
else
set2.erase(set2.find(top));
adjust();
}
}else if(strcmp(ch,"PeekMedian")==0){
if(stk.empty())
printf("Invalid\n");
else
printf("%d\n",mid);
}else if(strcmp(ch,"Push")==0){
scanf("%d",&x);
stk.push(x);
if(stk.empty()){
set1.insert(x);
mid=x;
}else if(x<=mid)
set1.insert(x);
else
set2.insert(x);
adjust();
}else
printf("Invalid\n");
}
return 0;
}
Stack (30)(模拟栈,输出中间数用set)的更多相关文章
- c语言学习,模拟栈操作
1.stack.c模拟栈操作函数的实现 #include<stdio.h> #include<stdlib.h> ; static char *stack;//数据栈 ;//栈 ...
- java模拟栈的操作
栈是一种有序列表,可以使用数组的结构来储存栈的数据内容 思路 1. 创建一个栈类StackArray 2. 定义一个top来模拟栈顶,初始化为-1 3. 入栈: 当有数据加入到栈的时候 top++ s ...
- ACM/ICPC 之 用双向链表 or 模拟栈 解“栈混洗”问题-火车调度(TSH OJ - Train)
本篇用双向链表和模拟栈混洗过程两种解答方式具体解答“栈混洗”的应用问题 有关栈混洗的定义和解释在此篇:手记-栈与队列相关 列车调度(Train) 描述 某列车调度站的铁道联接结构如Figure 1所示 ...
- 第一回写的用arraylist模拟栈操作
package hashMap; import java.util.ArrayList; import d.Student; /** * 用ArrayList模拟栈操作 * @author zhuji ...
- HDOJ/HDU 1022 Train Problem I(模拟栈)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- 使用golang的slice来模拟栈
slice(切片):底层数据结构是数组 stack(栈):一种先进后出的数据结构 普通版的模拟写入和读取的栈 package main import "fmt" //栈的特点是先进 ...
- 【PAT甲级】1057 Stack (30 分)(分块)
题意: 输入一个正整数N(<=1e5),接着输入N行字符串,模拟栈的操作,非入栈操作时输出中位数.(总数为偶数时输入偏小的) trick: 分块操作节约时间 AAAAAccepted code: ...
- java 16 - 5 LinkedList模拟栈数据结构的集合
请用LinkedList模拟栈数据结构的集合,并测试 题目的意思是: 你自己的定义一个集合类,在这个集合类内部可以使用LinkedList模拟. package cn_LinkedList; impo ...
- 【DataStructure In Python】Python模拟栈和队列
用Python模拟栈和队列主要是利用List,当然也可以使用collection的deque.以下内容为栈: #! /usr/bin/env python # DataStructure Stack ...
随机推荐
- windows 下部署 .netcore 到 windows service
接上一篇 <windows 下部署 .netcore 到 iis>,这一篇记录一下怎么将 Asp.Net Core 以 windows 服务的方式部署. 一.修改代码 其实也很简单,只要调 ...
- 【Python笔记】2020年7月22日练习=[定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程的两个解]
学习教程:廖雪峰-Python教程-函数-函数定义 学习记录:[定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程的两个解] 学习心得: 1.对问题进行判断分析后再下手. ...
- FCOS: Fully Convolutional One-Stage Object Detection
论文:FCOS: Fully Convolutional One-Stage Object Detection 目录 0.简介 1.网络结构 2.框回归--直接.自由 3.Center-ness ...
- 存储系列之 共享文件:链接link
一.link与unlink的定义 1.link link是Linux文件系统目录管理的一个系统调用,创建一个链接,该链接只是创建一个目录项,上文ext2的介绍中提到过目录项是<文件名,inode ...
- 多次调用Promise的then会返回什么?
//做饭 function cook(){ console.log('开始做饭.'); var p = new Promise(function(resolve, reject){ //做一些异步操作 ...
- Vue.js中传值给子部件及触发动作的问题
最近研究一个用vue.js做的程序并修改增加功能.其中用到传值给子部件等问题. template中有个子部件: <template> ...... <child-form v-if ...
- PAT 2-09. 装箱问题模拟(20)
题目链接 :http://www.patest.cn/contests/ds/2-09 解题思路:直接模拟, 记录已经使用的箱子的剩余容量, 如果已经使用的箱子中没有可以放下物品的箱子, 在增加另一个 ...
- 简单的股票信息查询系统 1 程序启动后,给用户提供查询接口,允许用户重复查股票行情信息(用到循环) 2 允许用户通过模糊查询股票名,比如输入“啤酒”, 就把所有股票名称中包含“啤酒”的信息打印出来 3 允许按股票价格、涨跌幅、换手率这几列来筛选信息, 比如输入“价格>50”则把价格大于50的股票都打印,输入“市盈率<50“,则把市盈率小于50的股票都打印,不用判断等于。
'''需求:1 程序启动后,给用户提供查询接口,允许用户重复查股票行情信息(用到循环)2 允许用户通过模糊查询股票名,比如输入“啤酒”, 就把所有股票名称中包含“啤酒”的信息打印出来3 允许按股票价格 ...
- 数据隐私和GDPR
近十几年来,随着大数据给各行各业带来的变化,以及数据时代不断强调的数据就是燃料,谁掌握数据谁就掌握未来的各种论调,大家纷纷开始收集数据,挖掘数据,转卖数据.而个人,作为数据真正拥有者的利益早就在商业利 ...
- rcp
rcp的命令 rcp,远程文件复制.rcp既可以在本地主机和远程主机之间进行文件复制,也可以用于在两个远程主机之间进行文件复制.除了复制文件外,rcp也可以用于目录复制,只需要加参数-r. rcp的命 ...