超时算法,利用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. 父容器根据子容器高度自适应:设置父容器 height:100%;overflow:hidden;

    父容器根据子容器高度自适应:设置父容器  height:100%;overflow:hidden;

  2. java 读取文件——按照行取出(使用BufferedReader和一次将数据保存到内存两种实现方式)

    1.实现目标 读取文件,将文件中的数据一行行的取出. 2.代码实现 1).方式1: 通过BufferedReader的readLine()方法. /** * 功能:Java读取txt文件的内容 步骤: ...

  3. Linux rpmbuild命令

    一.简介 rpmbuild命令用于创建软件的二进制包和源代码包. 二.选项 参考:http://blog.sina.com.cn/s/blog_4ba5b45e0102e5r2.html http:/ ...

  4. PHP Redis

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Myredis { //redis所 ...

  5. 三:shell运算符

    1:declare命令 shell默认是字符串型        如果需要加减乘除,需要定义为整型declare命令:改变默认变量的类型decalre [+/-] [选项]  变量名      -给变量 ...

  6. linux chmod命令和chown命令

    一.chmod及文件权限 1.了解文件权限 root账户新建一个目录permission,在该目录新建一个文件file,通过ll就可以查看其权限. root@development:~# cd per ...

  7. #include <NOIP2010 Junior> 三国游戏 ——using namespace wxl;

    题目描述 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏. 在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有 N 位武将(N为偶数且不小于 4),任意两个武将之 ...

  8. 【转】selenium学习路线

    selenium学习路线 配置你的测试环境,真对你所学习语言,来配置你相应的selenium 测试环境.selenium 好比定义的语义---“问好”,假如你使用的是中文,为了表术问好,你的写法是“你 ...

  9. 三维网格形变算法(Gradient-Based Deformation)

    将三角网格上的顶点坐标(x,y,z)看作3个独立的标量场,那么网格上每个三角片都存在3个独立的梯度场.该梯度场是网格的微分属性,相当于网格的特征,在形变过程中随控制点集的移动而变化.那么当用户拖拽网格 ...

  10. 第16章 调色板管理器_16.4 一个DIB位图库的实现(2)

    //接上一篇 //DibPal.h /*----------------------------------------------------------------- DIBPAL.H heade ...