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 内存管理机制:04 Java 内存分配策略
    Java 内存管理机制:03 垃圾收集器
    Java 内存管理机制:02 OOM异常
    蓝桥杯参赛笔记
    字符串和字符数组的输入输出种类对比
    centos6打开firewall遇到unit firewall.service is masked
    centos6打开防火墙时遇到Reason: No such file or directory
    linux配置文件
    试题 算法训练 素因子去重
    试题 算法训练 最大质因数
  • 原文地址:https://www.cnblogs.com/8023spz/p/9148054.html
Copyright © 2011-2022 走看看