PAT A 甲级 1057 Stack (30分)

1057 Stack (30分)

吐槽

……这题让我找回了刚开始练pat、STL都还不会用的阶段——总是报各种各样的错误而完全没有修改的头绪。

思路

先提一嘴scanf的事。这道题其实用cin时间也是够的,只不过会从50ms左右变成200+。总之不是必需。

然后是主干。
这道题非常容易超时。非常。
如果真的用vector保存数字,然后每次找中位数时都真的去找一下,又或者用两种sort方式分别处理pop和peek很容易想到都是会超时的。

用stack和set(multiset)分别保存两种不同排序方式的同一堆数算是比较好想到的方法了,毕竟set也有好用的find函数,可以比较容易的完成两者同时去掉某个数的操作。
但这里就有个问题了——由于set是红黑树链表性质的,所以它的迭代器不能像vector一样进行一次性加某个数值的操作,而只能多次自增来达到选中指定元素的效果,而这也会导致超时。

所幸,中位数是一个完全不需要全局信息的数——可以通过当前中位数与加入的数关系判断其加入后中位数应该左移右移或不动。

所以方法1也呼之欲出了——每次push和pop的时候,根据情况移动迭代器,使其一直指向中位数。

然而这个逻辑说实话,很复杂。我在纸上涂涂画画花了很久终于找到了这个变化逻辑,但写成代码后又开始出问题(而且是3个测点对2个不对的让人看不懂的方式)。

经女友提醒后,使用了现在的一个set small保存中位数以左(以及中位数)一个set big保存中位数以右的方式。每次push、pop操作后只需让程序自行平衡两边的数字(代码中的balance函数)即可。要平衡的有“小列中最大值高于大列中最小值”和“两者size差异不合规”两种情况。(我因为忘了前者多花了很多时间(摊手))

代码

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
#include<stack>

using namespace std;

multiset<int,greater<int>> small;
multiset<int> big;
multiset<int>::iterator it;

void balance()
{
	while (*small.begin()>*big.begin()||small.size() > big.size() + 1)
	{
		big.insert(*small.begin());
		small.erase(small.begin());
	}
	while (small.size() < big.size())
	{
		small.insert(*big.begin());
		big.erase(big.begin());
	}
}

int main()
{
	int n;
	cin >> n;
	int i, temp;
	stack<int> v;
	char s[15];
	for (i = 0; i < n; i++)
	{
		scanf("%s", s);
		if (s[1] == 'o')
		{
			if (v.empty())
				printf("Invalid\n");
			else
			{
				printf("%d\n", v.top());
				if (!big.empty() && big.find(v.top()) != big.end())
					big.erase(big.find(v.top()));
				else if (!small.empty() && small.find(v.top()) != small.end())
					small.erase(small.find(v.top()));
				balance();
				v.pop();
			}
		}
		else if (s[1] == 'e')
		{
			if (v.empty())
				printf("Invalid\n");
			else
			{
				printf("%d\n", *small.begin());
			}
		}
		else if (s[1] == 'u')
		{
			scanf("%d", &temp);
			v.push(temp);
            small.insert(temp);
			balance();
		}
	}
	cin >> i;
	return 0;
}

题目

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 (≤10
​5
​​ ). 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 10
​5
​​ .

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值