1057. Stack (30)
2016-07-16 21:58
453 查看
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 PeekMedian command, print in a line the corresponding returned value. If the command is invalid, print "Invalid" instead.
Sample Input:
Sample Output:
模拟stack,不过这里的stack加多一个功能,就是输出中位数。用一个stack数据结构来模拟栈的功能,而为了方便得到中位数,要同时维护两个multiset数据结构(因为值可以重复,所以要用multiset)。一个set储存前一半的数(设为s1),一个set储存后一半的数(设为s2),这里s1的大小要和s2的大小一样或比s2的大小大1。同时更新中位数值mid,mid就是s1的最后一个数。在push的时候,把值push到stack中,同时,如果push的值小于等于mid就插入到s1,否则插入到s2,最后为了使他们的大小符合上面的描述,所以要调整一下,更新s1,s2和mid。pop的时候,对stack进行pop操作,同时,如果pop出来的值小于等于mid,就在s1中找出该值然后删除,否则在s2中找出该值然后删除,当前最后也要调整一下,更新s1,s2和mid。最后找中位数就直接找就行了。这里要注意的是multiset删除操作中,不能用实值作为参数,因为这样会把所有的这个值都删去,这里要先用find函数找出其中一个然后再删除。
代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
#include <set>
using namespace std;
multiset<int>s1,s2;
int mid;
void adjust()
{
multiset<int>::iterator it;
if(s1.size()<s2.size())
{
it=s2.begin();
s1.insert(*it);
s2.erase(it);
}
if(s1.size()>s2.size()+1)
{
it=s1.end();
it--;
s2.insert(*it);
s1.erase(it);
}
if(!s1.empty())
{
it=s1.end();
it--;
mid=*it;
}
}
int main()
{
int n;
scanf("%d",&n);
stack<int>sta;
for(int i=0;i<n;i++)
{
char s[15];
scanf("%s",s);
if(strcmp(s,"Push")==0)
{
int num;
scanf("%d",&num);
sta.push(num);
if(s1.empty()) s1.insert(num);
else if(num<=mid) s1.insert(num);
else s2.insert(num);
adjust();
}
else if(strcmp(s,"Pop")==0)
{
if(sta.empty())
{
printf("Invalid\n");
}
else
{
int tmp=sta.top();
printf("%d\n",tmp);
sta.pop();
if(tmp<=mid)
{
s1.erase(s1.find(tmp));
}
else
{
s2.erase(s2.find(tmp));
}
adjust();
}
}
else if(strcmp(s,"PeekMedian")==0)
{
if(sta.empty())
{
printf("Invalid\n");
}
else
{
printf("%d\n",mid);
}
}
else
printf("Invalid\n");
}
}
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 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
模拟stack,不过这里的stack加多一个功能,就是输出中位数。用一个stack数据结构来模拟栈的功能,而为了方便得到中位数,要同时维护两个multiset数据结构(因为值可以重复,所以要用multiset)。一个set储存前一半的数(设为s1),一个set储存后一半的数(设为s2),这里s1的大小要和s2的大小一样或比s2的大小大1。同时更新中位数值mid,mid就是s1的最后一个数。在push的时候,把值push到stack中,同时,如果push的值小于等于mid就插入到s1,否则插入到s2,最后为了使他们的大小符合上面的描述,所以要调整一下,更新s1,s2和mid。pop的时候,对stack进行pop操作,同时,如果pop出来的值小于等于mid,就在s1中找出该值然后删除,否则在s2中找出该值然后删除,当前最后也要调整一下,更新s1,s2和mid。最后找中位数就直接找就行了。这里要注意的是multiset删除操作中,不能用实值作为参数,因为这样会把所有的这个值都删去,这里要先用find函数找出其中一个然后再删除。
代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
#include <set>
using namespace std;
multiset<int>s1,s2;
int mid;
void adjust()
{
multiset<int>::iterator it;
if(s1.size()<s2.size())
{
it=s2.begin();
s1.insert(*it);
s2.erase(it);
}
if(s1.size()>s2.size()+1)
{
it=s1.end();
it--;
s2.insert(*it);
s1.erase(it);
}
if(!s1.empty())
{
it=s1.end();
it--;
mid=*it;
}
}
int main()
{
int n;
scanf("%d",&n);
stack<int>sta;
for(int i=0;i<n;i++)
{
char s[15];
scanf("%s",s);
if(strcmp(s,"Push")==0)
{
int num;
scanf("%d",&num);
sta.push(num);
if(s1.empty()) s1.insert(num);
else if(num<=mid) s1.insert(num);
else s2.insert(num);
adjust();
}
else if(strcmp(s,"Pop")==0)
{
if(sta.empty())
{
printf("Invalid\n");
}
else
{
int tmp=sta.top();
printf("%d\n",tmp);
sta.pop();
if(tmp<=mid)
{
s1.erase(s1.find(tmp));
}
else
{
s2.erase(s2.find(tmp));
}
adjust();
}
}
else if(strcmp(s,"PeekMedian")==0)
{
if(sta.empty())
{
printf("Invalid\n");
}
else
{
printf("%d\n",mid);
}
}
else
printf("Invalid\n");
}
}
相关文章推荐
- Mysql Error Code : 1436 Thread stack overrun
- 基于java中stack与heap的区别,java中的垃圾回收机制的相关介绍
- Python算法之栈(stack)的实现
- Stack数据结构的特点后进先出的应用:大数据运算
- C 堆栈,运行时类型挷定
- jstack和线程dump分析
- c语言stack实现--数据域与指针域分开
- 深入Java虚拟机
- 搭建CloudStack
- 新手,正在学Java Collection,瞎写点东西-一个基于链表的stack及其遍历
- 转贴一份挺好的堆和栈区别的资料
- java 关于stack 的语法解析
- STL学习记录(八)Sets、Multisets
- parse in programming
- stack的功能
- 用类模板改写stack
- 百田笔试题
- Stack链表C实现
- Stack--C++模板类
- Binary Tree Preorder Traversal