程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Pat(Advanced Level)Practice--1057(Stack)

Pat(Advanced Level)Practice--1057(Stack)

編輯:C++入門知識

Pat1057代碼

題目描述:

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

試了試最普通的做法,有三個case超時,果然卡了時間。
#include
#include
#include
#include
#define MAX 100005

using namespace std;

typedef struct Stack
{
	int data[MAX];
	int top;
}Stack;

void Init(Stack *s)
{
	s->top=0;
}

void Push(Stack *s,int d)
{
	int index=(++s->top);
	s->data[index]=d;
}

void Pop(Stack *s)
{
	if(s->top==0)
	{
		printf("Invalid\n");
	}
	else
	{
		int d=s->data[s->top];
		printf("%d\n",d);
		s->top--;
	}
}

void PeekMedian(Stack *s)
{
	int temp[MAX];
	for(int i=1;i<=s->top;i++)
		temp[i]=s->data[i];
	sort(temp+1,temp+1+s->top);
	if(s->top==0)
		printf("Invalid\n");
	else
	{
		if((s->top)%2==0)
		{
			printf("%d\n",temp[(s->top)/2]);
		}
		else
		{
			printf("%d\n",temp[(s->top+1)/2]);
		}
	}
}

int main(int argc,char *argv[])
{
	char str[20];
	int i,n;
	Stack s;
	Init(&s);
	scanf("%d",&n);
	getchar();
	for(i=0;i因為操作中需要不斷的求中位數,所以這樣復制,排序很自然地導致了超時,改用樹狀數組就可以AC了;
AC代碼:
#include
#include
#include
#define MAX 100005

using namespace std;

int TreeArray[MAX];//樹狀數組裡A[i]的值,表示i出現的次數

int lowbit(int n)
{
	return n&(-n);
}

void update(int n,int num)
{
	while(n<=MAX)
	{
		TreeArray[n]+=num;
		n+=lowbit(n);
	}
}

int GetSum(int n)
{
	int sum=0;
	while(n>0)
	{
		sum+=TreeArray[n];
		n-=lowbit(n);
	}
	return sum;
}

int BinSearch(int target)//對棧中間元素進行二分查找,GetSum(mid)返回的值
{                        //表示0~mid之間元素的個數,如果等於target,則mid
	int left=0,right=MAX;//就是要求的結果
	int mid;
	while(left<=right)
	{
		mid=left+(right-left)/2;
		int sum=GetSum(mid);
		if(sum>target)
			right=mid-1;
		else if(sum s;
	char str[20];
	int i,n;
	scanf("%d",&n);
	getchar();
	for(i=0;i最後發現網上還流傳一個比較巧妙的方法,用multiset維護兩個集合,這樣也可以很快的取出中位數,非常巧妙!!!
AC代碼:
#include
#include
#include
#include
#include

using namespace std;

stack s;
multiset upper;//大於中位數的數,從小到大排列
multiset > lower;//小於中位數的數,從大到小排列
int mid=0;

void Adjust(int* mid)//調整中位數
{
	if(upper.size()>lower.size())
	{
		lower.insert(*upper.begin());
		upper.erase(upper.begin());
	}
	else if(lower.size()>upper.size()+1)
	{
		upper.insert(*lower.begin());
		lower.erase(lower.begin());
	}
	(*mid)=*lower.begin();
}

int main(int argc,char *argv[])
{
	int i,n;
	char str[20];
	scanf("%d",&n);
	getchar();
	for(i=0;i*lower.begin())
					{
						upper.erase(upper.find(key));
					}
					else
					{
						lower.erase(lower.find(key));
					}
					if(s.empty())
						mid=0;
					else
						Adjust(&mid);
				}
				break;
			case 'e':
				if(s.empty())
					printf("Invalid\n");
				else
					printf("%d\n",mid);
				break;
			case 'u':
				int key;
				scanf("%d",&key);
				s.push(key);
				if(key>mid)
					upper.insert(key);
				else
					lower.insert(key);
				Adjust(&mid);
				break;
		}
	}

	return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved