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

    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 (-th smallest element if N is even, or (-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 (≤). 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 1.

    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


    这题真心可以,一开始没想那么多,写的有点暴力,结果三组超时,
    竟然要用到高级算法,emmmm。。。。这不是超纲了嘛。
    虽然对我来说不超纲,但是这对其他人有点难确实。
    树状数组可还行,之前想了一下每次都要重新求前缀和,再加上要排序,干脆用个树状数组方便。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 #define mod 100005
     4 int n;
     5 int m;
     6 string s;
     7 deque<int> q;
     8 int tree[mod];
     9 
    10 int lowbit(int x){
    11     return x&(-x);
    12 }
    13 
    14 void add(int pox, int val){
    15     while(pox < mod){
    16         tree[pox] += val;
    17         pox += lowbit(pox);
    18     }
    19 }
    20 
    21 int getsum(int x){
    22     int sum = 0;
    23     while(x > 0){
    24         sum += tree[x];
    25         x -= lowbit(x);
    26     }
    27     return sum;
    28 }
    29 
    30 int binary_search(int val){
    31     val = (val+1)>>1;
    32     int l = 1, r = 100000, mid;
    33     while(l < r){
    34         mid = (l+r)>>1;
    35         if(getsum(mid) < val){
    36             l = mid + 1;
    37         }else{
    38             r = mid;
    39         }
    40     }
    41     return r;
    42 }
    43 
    44 
    45 int main(){
    46     // cin >> n;
    47     scanf("%d", &n);
    48     for(int i = 0; i < n; i++){
    49         cin >> s;
    50         if(s=="Pop"){
    51             if(!q.empty()){
    52                 printf("%d
    ", q.front());
    53                 add(q.front(), -1);
    54                 q.pop_front();
    55             }else{
    56                 printf("Invalid
    ");
    57             }
    58         }else if(s=="PeekMedian"){
    59             if(!q.empty()){
    60                 int len = q.size();
    61                 int it = binary_search(len);
    62                 printf("%d
    ", it);
    63             }else{
    64                 printf("Invalid
    ");
    65             }
    66         }else{
    67             // cin >> m;
    68             scanf("%d", &m);
    69             q.push_front(m);
    70             add(m,1);
    71         }
    72     }
    73     return 0;
    74 }





  • 相关阅读:
    JQuery上传插件Uploadify使用详解
    jquery easyui datagrid使用参考
    easyui datagrid使用(好)
    灵活运用 SQL SERVER FOR XML PATH
    C# HttpRequest 中文编码问题
    echarts简单使用
    [bootstrap] 修改字体
    css :not 选择器
    [win7] 带网络的安全模式,启动QQEIMPlatform第三方服务
    [mysql] 添加用户,赋予不同的管理权限
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11190693.html
Copyright © 2011-2022 走看看