zoukankan      html  css  js  c++  java
  • 1057. Stack (30)

    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
    
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<vector>
     4 #include<algorithm>
     5 using namespace std;
     6 int cards[318];
     7 int Hash[100100];
     8 int main()
     9 {
    10     int n;
    11     scanf("%d",&n);
    12     vector<int> vv;
    13     for (int i = 0 ;i < n; ++i)
    14     {
    15         char str[11];
    16         int val;
    17         scanf("%s",str);
    18         if (strcmp(str,"Pop")==0)
    19         {
    20             if(vv.empty())
    21             {
    22                 printf("Invalid
    ");
    23             }
    24             else
    25             {
    26                 printf("%d
    ",vv[vv.size()-1]);
    27                 int key = vv[vv.size()-1];
    28                 vv.pop_back();
    29                 int index = key / 318;
    30                 --cards[index];
    31                 --Hash[key];
    32             }
    33         }
    34         else if (strcmp(str,"Push")==0)
    35         {
    36             scanf("%d",&val);
    37             vv.push_back(val);
    38             int index = val / 318;
    39             ++cards[index];
    40             ++Hash[val];
    41         }
    42         else if (strcmp(str,"PeekMedian")==0)
    43         {
    44             if(vv.empty())
    45             {
    46                 printf("Invalid
    ");
    47             }
    48             else
    49             {
    50                 int all = (vv.size()+1) / 2 ;
    51                 int sum = 0 , index = 0;
    52                 while(sum + cards[index] < all)
    53                 {
    54                     sum += cards[index];
    55                     ++ index;
    56                 }
    57                 int bg = index * 318 - 1;
    58                 sum = all - sum;
    59                 while(sum > 0)
    60                 {
    61                     ++ bg;
    62                     if(Hash[bg] > 0)
    63                     {
    64                         sum -= Hash[bg];
    65                     }    
    66                 }
    67                 printf("%d
    ",bg);
    68             }
    69         }
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    [Javascript]发布一个自己写的日期控件:DateTimeList
    Oracle PL/SQL 编程手册(SQL大全)
    [乱七八糟][转]程序版吉祥三宝
    [乱七八糟][转]这不是你想象中的软件产业
    [随文杂记]生男好还是生女好?
    [SqlServer]链接数据库存储过程
    [音乐天堂]辛德勒名单原声大碟
    [C#]WinFrom中的DataGrid单击选择行
    [乱七八糟]《进化论——人类科学史上最大的谎言》
    [乱七八糟]《阿甘正传》点评
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/5188507.html
Copyright © 2011-2022 走看看