pat1057. Stack (30)

Source: Internet
Author: User

1057. Stack (30) time limit MS Memory limit 65536 KB code length limit 16000 B procedure StandardAuthor Chen, Yue

The Stack is one of the most fundamental data structures, which are 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 is supposed to implement a stack with an extra operation:peekmedian-return the median value of all the Elemen TS in the stack. With n elements, the median value was defined to being 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 the 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
QoQ
Peekmedian

Where key is a positive integer no more than 105.

Output Specification:

For each Push command, insert key to 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:
17PopPeekMedianPush 3PeekMedianPush 2PeekMedianPush 1PeekMedianPopPopPush 5Push 4PeekMedianPopPopPopPop
Sample Output:
Invalidinvalid322124453invalid

Submit Code

Note the point:

1. Common operations for tree arrays

Learn:

Http://www.cnblogs.com/zhangshu/archive/2011/08/16/2141396.html

http://blog.csdn.net/eli850934234/article/details/8863839

1 intLowbit (intval) {2     returnval& (-val);3 }4 intSumintval) {5     intres=0;6      while(val>0){7res+=Line[val];8val-=Lowbit (val);9     }Ten     returnRes; One } A voidAddintValintx) { -      while(val<maxnum) { -line[val]+=x; theval+=Lowbit (val); -     } - } - intFindintval) { +     intL=1, r=Maxnum,mid,res; -      while(L<r) {//not on the right . +Mid= (L+R)/2; Ares=sum (mid); at         if(res<val) { -L=mid+1;//(1) -         } -         Else{ -R=mid;//[l,r) Each point of the sum, must be less than sum (R), and eventually must end at (1). Finally returns L.  -         } in     } -     returnl; to}

Pay attention to the two-point search here. For interval [A, b], keep two points, and finally return L.

The operation of 2.string is generally longer than that of char. The string used here will time out.

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <stack>5#include <Set>6#include <map>7#include <algorithm>8 using namespacestd;9 #defineMaxnum 100005Ten intLine[maxnum]; One intLowbit (intval) { A     returnval& (-val); - } - intSumintval) { the     intres=0; -      while(val>0){ -res+=Line[val]; -val-=Lowbit (val); +     } -     returnRes; + } A voidAddintValintx) { at      while(val<maxnum) { -line[val]+=x; -val+=Lowbit (val); -     } - } - intFindintval) { in     intL=1, r=Maxnum,mid,res; -      while(L<r) {//not on the right . toMid= (L+R)/2; +res=sum (mid); -         if(res<val) { theL=mid+1; *         } $         Else{Panax NotoginsengR=mid; -         } the     } +     returnl; A } the intMain () { +     //freopen ("D:\\input.txt", "R", stdin); -     intN,num; $     Charop[ the]; $     //string op; -stack<int>s; -scanf"%d",&n); the      while(n--){ -scanf"%s", op);//cin>>op;Wuyi         if(op[1]=='u'){ thescanf"%d",&num); - s.push (num); WuAdd (num,1); -         } About         Else{ $             if(op[1]=='o'){ -                 if(S.empty ()) { -printf"invalid\n"); -                 } A                 Else{ +num=s.top (); the S.pop (); -printf"%d\n", num); $Add (num,-1); the                 } the             } the             Else{ the                 if(s.size () = =0){ -printf"invalid\n"); in                     Continue; the                 } the                 if(S.size ()%2==0){ AboutNum=find (S.size ()/2); the                 } the                 Else{ theNum=find ((s.size () +1)/2); +                 } -printf"%d\n", num); the             }Bayi         } the     } the     return 0; -}

pat1057. Stack (30)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.