超时算法,利用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. LeetCode#227.Basic Calculator II

    题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...

  2. linux下文件的特殊权限s和t

    先看看这两个文件的权限:[root@localhost ~]# ls -ld /usr/bin/passwd /tmpdrwxrwxrwt 4 root root 4096 Jun 2 17:33 / ...

  3. [转载]python脚本删除一定时间以外的文件

    import os; import sys; import time; class DeleteLog: def __init__(self,filename,days): self.filename ...

  4. android 判断字符串是否为空与比对["=="与equals()的区别]

    if (s == null || s.equals("")) ; } s.equals("")里面是要比对的字符串 声明字符串未赋初始值或值,然后比对就会出错, ...

  5. mysql行锁和表锁

    mysql innodb支持行锁和表锁,但是MyIsam只支持表锁.现在我们说说mysql innodb的行锁和 有如下表id为主键 为了出现演示效果,我们将mysql的autocommit设置为0 ...

  6. 移动windows live writer文章的保存路径

    windows live writer强制安装在C盘,文章也是强制保存在我的文档中.那么我们想办法来改变保存的路径,防止重装系统的时候忘记保存C盘的东西. 网上找到的参考:http://www.dit ...

  7. Ubuntu Kylin15下PHP环境的搭建(LAMP)

    Ubuntu下的PHP开发环境架设   今天重新装了ubuntu那么就吧过程记录下. 打开终端,也就是命令提示符. 我们先来最小化组建安装,按照自己的需求一步一步装其他扩展.命令提示符输入如下命令: ...

  8. monkeyrunner之安卓开发环境搭建(一)

    在学习monkeyrunner之前,让我们先搭建好eclipse安卓开发环境. 对于程序开发人员而言,eclipse并不陌生,它提供了一个非常广阔的平台来开发程序.同样也可以用它来开发android程 ...

  9. [转]菜鸟程序员之Asp.net MVC Session过期异常的处理

    本文转自:http://www.cnblogs.com/JustRun1983/p/3377652.html 小赵是刚毕业的计算机专业方面的大学生,4年的大学时间里面,他读过了很多编程方面的数据,也动 ...

  10. 自定义评分器Similarity,提高搜索体验(转)

    文章转自:http://blog.csdn.net/duck_genuine/article/details/6257540 首先说一下lucene对文档的评分规则: score(q,d)   =   ...