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;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

black-hole6

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值