zoukankan      html  css  js  c++  java
  • 1057 Stack (30)(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 (<= 10^5^). 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 10^5^.

    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
    

    树状数组,二分查找,找到
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    #define MAX 100000
    using namespace std;
    
    int n,d,s[MAX],tree[MAX + 1],c;
    char ope[11];
    int lowbit(int t) {
        return t&-t;
    }
    void update(int x,int y) {
        while(x <= MAX) {
            tree[x] += y;
            x += lowbit(x);
        }
    }
    int getsum(int x) {
        int ans = 0;
        while(x > 0) {
            ans += tree[x];
            x -= lowbit(x);
        }
        return ans;
    }
    void printM() {
        int m = (c % 2 ? c / 2 + 1 : c / 2);///中间个数应该是第几个数
        int l = 0,r = MAX,mid;
        while(l < r) {
            mid = (l + r) / 2;
            if(getsum(mid) < m)l = mid + 1;
            else r = mid;
        }
        printf("%d
    ",l);
    }
    int main() {
        scanf("%d",&n);
        while(n --) {
            scanf("%s",ope);
            if(ope[1] == 'u') {
                scanf("%d",&d);
                update(s[c ++] = d,1);
            }
            else if(ope[1] == 'o') {
                if(!c) {
                    printf("Invalid
    ");
                }
                else {
                    update(s[-- c],-1);
                    printf("%d
    ",s[c]);
                }
            }
            else {
                if(!c) {
                    printf("Invalid
    ");
                }
                else {
                    printM();
                }
            }
        }
    }
    View Code
    个数对应中间位置的最小的。


  • 相关阅读:
    杂谈-论时间成本
    java笔记:排错5:误删maven target:恢复不了,怎么再生成
    十大经典排序(转发帖)
    java笔记(Idea,Maven):误删maven项目的target的class,怎么再生成target
    JDK解压版制作
    日常报错记录4:ssh工程复制粘贴顺序。
    欧朋Opera 浏览器(打不开百度)提示“您的连接不是私密连接”,解决办法
    日常报错记录2: MyBatis:DEBUG [main]
    JavaWeb-SQL-Servlet-JSP学做购物系统——日志一
    git 命令
  • 原文地址:https://www.cnblogs.com/8023spz/p/9148054.html
Copyright © 2011-2022 走看看