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 keywhere 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 PopSample Output:
Invalid Invalid 3 2 2 1 2 4 4 5 3 Invalid
#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; }