超时算法,利用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. OpenStack 企业私有云的若干需求(7):电信行业解决方案 NFV

    自动扩展(Auto-scaling)支持 多租户和租户隔离 (multi-tenancy and tenancy isolation) 混合云(Hybrid cloud)支持 主流硬件支持.云快速交付 ...

  2. .Net程序员之Python基础教程学习----列表和元组 [First Day]

    一. 通用序列操作: 其实对于列表,元组 都属于序列化数据,可以通过下表来访问的.下面就来看看序列的基本操作吧. 1.1 索引: 序列中的所有元素的下标是从0开始递增的. 如果索引的长度的是N,那么所 ...

  3. Java Web之会话技术

    客户端与服务器通信过程中,会产生一些数据.比如,A和B分别登陆了某宝购物网站,A买了一个Android手机,B买了一个iPhone手机,当结账时,web服务器需要分别对用户A和B的信息分别保存.根据J ...

  4. 二分+最短路判定 BZOJ 2709: [Violet 1]迷宫花园

    BZOJ 2709: [Violet 1]迷宫花园 Sample Input 5 ######### # # # # # # # #S# # ##### # # ## # # # ### ### ## ...

  5. VR可以用做除游戏外的哪些地方

    虚拟现实可以将你传送到平时不敢去的地方, 例如喜马拉雅山顶. 也可以将你带到人类几乎不可能到达的地方, 例如世界上最深处, 马里亚纳海沟.马里亚纳海沟最深处的水压可是达到惊人的110Mpa, 这个高压 ...

  6. 添加事件及Event对象的兼容写法

    一.事件流 事件流描述的是从页面中接受事件的顺序. IE的事件流是事件冒泡流,而Netscape的事件流是事件捕获流 1.事件冒泡 事件冒泡,即事件最开始由最具体的元素(文档中嵌套层次最深的那个节点) ...

  7. [No00004D]深度思考好文:软件工程师的困境

    昨天是我一同学结婚的好日子,同学们大家聊各自的工作,有个同学突然问了我一句:我们同学中好像做软件的不多?如果再细分,好像做网络相关的更少? 回想起当时为何读计算机信息管理的专业,是因为那时听说读电脑未 ...

  8. java 26 - 8 网络编程之 TCP协议的练习

    TCP练习: 1.客户端键盘录入,服务器输出文本文件 客户端代码: public class ClientDemo { public static void main(String[] args) t ...

  9. Mysql连接错误:Lost connection to Mysql server at 'waiting for initial communication packet'

    在远程连接mysql的时候,连接不上,出现如下报错:Lost connection to MySQL server at 'waiting for initial communication pack ...

  10. 使用 JavaScript File API 实现文件上传

    概述 以往对于基于浏览器的应用而言,访问本地文件都是一件头疼的事情.虽然伴随着 Web 2.0 应用技术的不断发展,JavaScript 正在扮演越来越重要的角色,但是出于安全性的考虑,JavaScr ...