超时算法,利用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. mysql行锁和表锁

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

  2. vsftpd 配置:chroot_local_user与chroot_list_enable详解

    chroot_local_user,chroot_list_enable,chroot_list_file三个配置项的解释: chroot_local_user #是否将所有用户限制在主目录,YES为 ...

  3. linux下 ^M

    在Linux下使用vi来查看一些在Windows下创建的文本文件,有时会发现在行尾有一些“^M”.有几种方法可以处理. 注意:在Linux下,可以通过ctrl+v,ctrl+m,打出^M字符.而却,以 ...

  4. 嵌入式Linux开发板

    嵌入式Linux开发板开发介绍: iTOP-4412嵌入式Linux开发板搭载三星Exynos四核处理器,配备1GB内存,4GB固态硬盘EMMC存储,独家配备三星S5M8767电源管理,配备Andro ...

  5. MMORPG大型游戏设计与开发(客户端架构 part4 of vegine)

    昨天是七夕,祝大家都过的快乐,希望这句迟到的问候不会造成大家心中的困扰.这一节讲到了前端比较重要的模块,性能以及调试异常模块.一个应用的性能往往是最核心的部分,就像人身体的各个器官一样,一小部分也不能 ...

  6. Binary Tree: Write a function to return count of nodes in binary tree which has only one child.

    June 8, 2015 我最喜欢的一道算法题目, 二行代码. 编程序需要很强的逻辑思维, 严密,我还没有很好训练自己.想一想, 二行代码, 五分钟就可以搞定; 最近这几天网上大家热议的 Homebr ...

  7. CodeForces 466E Information Graph --树形转线性+并查集

    题意:有三种操作: 1.新增一条边从y连向x,此前x没有父节点 2.x接到一份文件,(文件标号逐次递增),然后将这份文件一路上溯,让所有上溯的节点都接到这份文件 3.查询某个节点x是否接到过文件F 解 ...

  8. Prefab Assist插件

    资料 Prefab文档: http://game.ceeger.com/Manual/Prefabs.html 基础:基于NGUI制作组件的Prefab 前言 在一个游戏的UI中,有一些通用的组件,比 ...

  9. WCF添加服务失败一则

    原因是本机开发IIS没有安装HTTPS证书 将红色的字注释掉就好了! <services> <service behaviorConfiguration="basicSer ...

  10. 使用adb shell卸载程序

    个人感觉在命令行中卸载程序要比在手机界面卸载程序要方便许多,配合命令行下的报名查看包名的命令就更加方便了. 1.查看应用准确包名 adb shell pm list package -f |grep ...