超时算法,利用2的特殊性,用2个multiset来维护。单个multiset维护没法立即找到中位数。

其实也可以只用1个multiset,用一个中位指针,++,--来维护中位数。

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 7;
int n;
multiset<int> up, lo;
stack<int> sta;
void adapt() {
    int cnt = up.size() + lo.size();
    int upsz = ceil(cnt / 2.0);
    while (up.size() < upsz) {
        up.insert(*lo.begin());
        lo.erase(lo.begin());
    }
    while (up.size() > upsz) {
        int x = *up.rbegin();
        lo.insert(x);
        up.erase(up.find(x));
    }
}
void push(int x) {
    up.insert(x);
    adapt();
}
int peek() {
    return *(up.rbegin());
}
void pop(int x) {
    if (up.find(x) != up.end())
        up.erase(up.find(x));
    else
        lo.erase(lo.find(x));
    adapt();
}
int main() {
    freopen("in.txt", "r", stdin);
    cin >> n;
    while (sta.empty() == false)
        sta.pop();
    up.clear(), lo.clear();
    char cmd[13];
    int x;
    while (n--) {
        cin >> cmd;
        if (cmd[1] != 'u') {
            if (lo.empty() && up.empty()) {
                puts("Invalid");
                continue;
            }
            if (cmd[1] == 'o') {
                int x = sta.top();
                sta.pop();
                cout << x << endl;
                pop(x);
            } else if (cmd[1] == 'e') {
                cout << peek() << endl;
            }
        } else {
            cin >> x;
            sta.push(x);
            push(x);
        }
    }
    return 0;
}

此题正解树状数组

#include<stdio.h>
#include<cstring>
#include<iostream>
#include<string>
using namespace std;  

;
int c[N];  

int lowbit(int i){
    return i&(-i);
}  

void add(int pos,int value){
    while(pos<N){
        c[pos]+=value;
        pos+=lowbit(pos);
    }
}  

int sum(int pos){
    ;
    ){
        res+=c[pos];
        pos-=lowbit(pos);
    }
    return res;
}  

int find(int value){
    ,r=N-,median,res;
    ){
        ==)
            median=(l+r)/;
        else
            median=(l+r-)/;
        res=sum(median);
        if(res<value)
            l=median;
        else
            r=median;  

    }
    ;
}  

int main(){
    //freopen("D://test.txt","r",stdin);
    ];
    ,n,pos;
    memset(c,,sizeof(c));
    scanf("%d",&n);
    while(n--){
        scanf("%s",ss);
        ]=='u'){
            scanf("%d",&pos);
            stack[++top]=pos;
            add(pos,);
        }]=='o'){
            ){
                printf("Invalid\n");
                continue;
            }
            int out=stack[top];
            add();
            printf("%d\n",stack[top--]);
        }]=='e'){
            ){
                printf("Invalid\n");
                continue;
            }
            int res;
            ==)
                res=find(top/);
            else
                res=find((top+)/);
            printf("%d\n",res);  

        }else{
            printf("Invalid\n");
        }
    }  

    ;
}  

类似题目:zoj3612

pat1057 stack的更多相关文章

  1. pat1057. Stack (30)

    1057. Stack (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Stack is one of ...

  2. PAT1057 Stack(树状数组+倍增)

    目录 题目大意 题目分析 题目大意 要求维护一个栈,提供压栈.弹栈以及求栈内中位数的操作(当栈内元素\(n\)为偶数时,只是求第\(n/2\)个元素而非中间两数的平均值).最多操作100000次,压栈 ...

  3. PAT1057 stack(分块思想)

    1057 Stack (30分)   Stack is one of the most fundamental data structures, which is based on the princ ...

  4. PAT-1057 Stack (树状数组 + 二分查找)

    1057. Stack Stack is one of the most fundamental data structures, which is based on the principle of ...

  5. PAT甲级1057. Stack

    PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...

  6. 线性数据结构之栈——Stack

    Linear data structures linear structures can be thought of as having two ends, whose items are order ...

  7. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  8. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  9. Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder

    Stack Overflow 排错翻译  - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...

随机推荐

  1. linux学习之路——ubuntu 16.04 开机开启数字小键盘解决方法

    第一步:安装numlockx,输入命令 sudo apt-get install numlockx 第二步:用 vim 打开 rc.local 文件,输入命令 sudo vim /etc/rc.loc ...

  2. TCP连接建立系列 — 服务端接收ACK段(一)

      http://blog.csdn.net/zhangskd/article/details/17923917 分类: Linux TCP/IP Linux Kernel 2014-01-07 09 ...

  3. MT7620a openwrt barrier_breaker编译后wan口dhcp无法获得地址

    前言 我司准备使用openwrt barrier_breaker版本做二次开发.在烧入固件后发现wan口,dhcp无法获得地址.经如下修改后,mt7620a的路由器可以正常获得地址. 修改dts文件 ...

  4. 实例浅析epoll的水平触发和边缘触发,以及边缘触发为什么要使用非阻塞IO

    一.基本概念                                                          我们通俗一点讲: Level_triggered(水平触发):当被监控的 ...

  5. [转]jqGrid 属性、事件全集

    本文转自:http://blog.csdn.net/rosanu_blog/article/details/8334070 以下是jqGrid 最常用的属性和事件,经过一段时间的整理,终于弄的差不多了 ...

  6. 大一上学期的一点小疑惑,代码验证ok

    #include<iostream> using namespace std; class test { int *p; int pn; public: test(int n) { p = ...

  7. Windows 2008 R2 64位上安装wamp失败的原因

    Exception Exception in module wampmanager.exe at 000F15A0... 因测试PHP程序需要,需要在windows系统上布署WAMP环境测试程序,对性 ...

  8. 如何在Kali Linux下编译Windows Exploit

    前言 微软的Windows在企业或是个人应用领域占据着最大的市场份额,在渗透测试过程中你会经常遇到很多Windows的工作站和服务器.另一方面,大多数渗透测试人员主要使用基于Linux的发行版渗透测试 ...

  9. window对象的screen详解

    screen.availHeight     返回屏幕的高度(不包括Windows任务栏)screen.availWidth     返回屏幕的宽度(不包括Windows任务栏)screen.colo ...

  10. jmeter(二)录制脚本

    对大多数刚开始接触性能测试的人来说,代码功力可能不是太好,我们可以通过工具,录制脚本来进行测试,以达到我们的目的 一般来讲,录制脚本有两种方法 一.利用badboy进行脚本录制 1.下载安装 badb ...