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 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:
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");
	}
}




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值