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
    个数对应中间位置的最小的。


  • 相关阅读:
    Android系统源代码下载
    Windows Embedded Compact 7初体验
    windowsmobile 开发环境
    Windows X64汇编入门(1)
    汇编语言的Hello World
    如何构建Win32汇编的编程环境(ONEPROBLEM个人推荐)
    音频视频解决方案:GStreamer/ffmpeg/ffdshow/directshow/vfw
    汇编开发环境
    DirectX
    关于DirectShow SDK 和Windows SDK,及DirectX SDK
  • 原文地址:https://www.cnblogs.com/8023spz/p/9148054.html
Copyright © 2011-2022 走看看