浙大pat | 牛客网甲级 1017 Stack (30) 实现中位数栈

题目描述

Stack is one of the most fundamental data structures, which isbased on the principle of Last In First Out (LIFO).  The basic operations include Push (insertingan element onto the top position) and Pop (deleting the top element).  Now you are supposed to implement a stackwith an extra operation: PeekMedian -- return the median value of all theelements 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 apositive 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 10
5.




输出描述:

For each Push command, insert key into the stack andoutput nothing.  For each Pop orPeekMedian 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

这一题在原始的栈的基础上要求实现输出中位数的功能,这一题的关键就是每一个数字都是正整数而且最大的数为十万

因此我们可以定义两个数组,一个用来存储栈里面的每一个数,第二个数组用来出错每一个数出现的个数,然后用两个值theNum和theNumIndex分别用来表示当前的中位数和当前中位数在和它相等的数里面是第几个

Push函数的实现如下

当当前数字个数为偶数的时候,当插入的数字比当前中位数小的话,当前中位数保持不变,当插入数字比当前中位数大的时候,当前中位数右移一位,当插入数字和当前中位数相等的时候,当前中位数右移一位

当前数字个数为奇数的时候,当插入的数字比当前中位数小的话,当前中位数左移一位,当插入数字比当前中位数大的时候,当前中位数保持不变,当插入数字和当前中位数相等的时候,当前中位数保持不变

Pop函数的实现类似上面

这一题我还犯了两个很大的错误,一是数组忘了初始化为0,第二个错误是数组的大小声明错了,明明是10万的数组我声明为了1万的数组

#include <iostream>
#include <string>
using namespace std;


int theStack[100003];
int theNumStore[100003] = {0};
int theNumSize=0;
int theNum = -1;
int theNumIndex = -1;

void _push(int n)
{
	if(theNumSize == 0)
	{
		theStack[theNumSize++] = n;
		theNumStore[n]++;
		theNum = n;
		theNumIndex = 0;
		return;
	}
	theStack[theNumSize++] = n;
	theNumStore[n]++;
	if((theNumSize-1)%2 == 0)
	{
		if(n>=theNum)
		{
			theNumIndex++;
			if(theNumIndex>=theNumStore[theNum])
			{
				int p =theNum+1;
				while(theNumStore[p]==0) p++;
				theNum = p;
				theNumIndex = 0;
			}
			return;
		}
		else
		{
			return;
		}
	}
	else
	{
		if(n<theNum)
		{
			theNumIndex--;
			if(theNumIndex<0)
			{
				int p =theNum-1;
				while(theNumStore[p]==0) p--;
				theNum = p;
				theNumIndex = theNumStore[p]-1;
			}
			return;
		}
		else
		{
			return;
		}
	}
}

int _pop()
{
	if(theNumSize==0) return -1;
	int theNumTmp;
	if(theNumSize == 1 ) 
	{
		theNumTmp = theStack[--theNumSize];
		theNumStore[theNumTmp]--;
		theNum=-1;
		theNumIndex=-1;
		return theNumTmp;
	}
		theNumTmp = theStack[--theNumSize];
		theNumStore[theNumTmp]--;
		if((theNumSize+1)%2==0)
		{
			if(theNumTmp>theNum)
			{
				return theNumTmp;
			}
			else if(theNumTmp == theNum)
			{
				if(theNumIndex>=theNumStore[theNum])
				{
					int p =theNum+1;
					while(theNumStore[p] == 0) p++;
					theNum =p;
					theNumIndex=0;
					return theNumTmp;
				}
				else
				{
					return theNumTmp;
				}
			}
			else
			{
				theNumIndex++;
				if(theNumIndex>=theNumStore[theNum])
				{
				    int p =theNum+1;
					while(theNumStore[p]==0) p++;
					theNum=p;
					theNumIndex=0;
					return theNumTmp;
				}
				return theNumTmp;
			}
			
		}
		else
		{
		     if(theNumTmp>=theNum)
			{
			    theNumIndex--;
			    if(theNumIndex<0)
			    {
    				int p=theNum-1;
    				while(theNumStore[p]==0) p--;
    				theNum=p;
    				theNumIndex=theNumStore[p]-1;
    				return theNumTmp;
    			}
    			return theNumTmp;
			}
			else
			{
				return theNumTmp;
			}
		}
}

int main()
{
   int N;
   string a;
   int b;
   cin>>N;
    for(int i=0;i<N;i++)
    {
    	cin>>a;
    	if(a=="Push")
    	{
	    	cin>>b;
	    	_push(b);
	    }
	    if(a=="Pop")
	    {
    		b=_pop();
    		if(b==-1)
    		cout<<"Invalid"<<endl;
    		else
    		cout<<b<<endl;
    	}
    	if(a=="PeekMedian")
    	{
	    	if(theNum==-1)
	    	cout<<"Invalid"<<endl;
	    	else
	    	cout<<theNum<<endl;
	    }
    }
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值