分析:

  考察树状数组 + 二分, 注意以下几点:

    1.题目除了正常的进栈和出栈操作外增加了获取中位数的操作, 获取中位数,我们有以下方法:

        (1):每次全部退栈,进行排序,太浪费时间,不可取。

        (2):题目告诉我们key不会超过10^5,我们可以想到用数组来标记,但不支持快速的统计操作。

        (3):然后将数组转为树状数组,可以快速的统计,再配上二分就OK了。

    2.二分中我们需要查找的是一点pos,sum(pos)正好是当前个数的一半,而sum(pos - 1)就不满足。

 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cctype>
#include <stack>
#include <map> using namespace std; const int Max_required = ; int tree_array[Max_required]; inline int lowbit(int x)
{
return x&(-x);
} void add(int x, int value)
{
while (x < Max_required)
{
tree_array[x] += value;
x += lowbit(x);
}
} int sum(int x)
{
int total = ;
while (x > )
{
total += tree_array[x];
x -= lowbit(x);
}
return total;
} int binary_find(int x)
{
int low = , high = Max_required, mid; while (low <= high)
{
mid = (low + high) >> ;
int total = sum(mid); if (total >= x)
high = mid - ;
else if (total < x)
low = mid + ;
}
return low;
} int main()
{
int n;
stack<int> st;
while (scanf("%d", &n) != EOF)
{
//st.clear();
memset(tree_array, , sizeof(tree_array)); char ch[];
while (n--)
{
scanf("%s", ch);
if (strcmp("Push", ch) == )
{
int pp;
scanf("%d", &pp);
st.push(pp);
add(pp, );
}
else if (strcmp("Pop", ch) == )
{
if (st.empty())
printf("Invalid\n");
else
{
printf("%d\n", st.top());
add(st.top(), -);
st.pop();
}
}
else if (strcmp("PeekMedian", ch) == )
{
int len = st.size();
if (len == ) {
printf("Invalid\n");
continue;
}
int res = -;
if (len % == )
res = binary_find((len + ) / );
else
res = binary_find(len / );
printf("%d\n", res);
}
}
}
return ;
}

1057. Stack (30)的更多相关文章

  1. PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

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

  2. pat 甲级 1057 Stack(30) (树状数组+二分)

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

  3. PAT 1057. Stack (30)

    题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1057 用树状数组和二分搜索解决,对于这种对时间复杂度要求高的题目,用C的输入输出显然更好 #i ...

  4. PAT (Advanced Level) 1057. Stack (30)

    树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...

  5. 1057. Stack (30) - 树状数组

    题目如下: Stack is one of the most fundamental data structures, which is based on the principle of Last ...

  6. PAT甲级题解-1057. Stack (30)-树状数组

    不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekM ...

  7. 1057 Stack (30)(30 分)

    Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...

  8. 【PAT甲级】1057 Stack (30 分)(分块)

    题意: 输入一个正整数N(<=1e5),接着输入N行字符串,模拟栈的操作,非入栈操作时输出中位数.(总数为偶数时输入偏小的) trick: 分块操作节约时间 AAAAAccepted code: ...

  9. 1057 Stack (30分)(树状数组+二分)

    Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...

随机推荐

  1. CrashHandler: java.lang.NullPointerException

    08-29 20:33:47.305 20636-20636/com.tongyan.subway.inspect D/AndroidRuntime: Shutting down VM 08-29 2 ...

  2. mybatis配置优化

    1.加入日志log4j 1)加入jar包:log4j-1.2.17.jar 2)加入log4j配置文件: 可以使properties或者xml形式 log4j.rootLogger = DEBUG,C ...

  3. 四则运算GUI设计

    使用了QT来设计GUI,目前也还在看视频学习中,目前还不会用这个软件,所以具体功能还未实现,初步绘制的界面如下:

  4. (LinkedList)Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  5. Blackfin DSP(六):BF533的SPORT接口

    1.特性 bf533有两个SPORT口(synchronous serial Port),即同步串行接口.完全独立的接收和发送通道,且每个通道都具有缓冲,最高速度可达SCLK/2.最大支持32bit字 ...

  6. 64. ZigZag Conversion

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

  7. JAVA设计模式之享元模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述享元(Flyweight)模式的: Flyweight在拳击比赛中指最轻量级,即“蝇量级”或“雨量级”,这里选择使用“享元模式”的意译,是 ...

  8. attr与prop

    Jquery获取checkbox属性checked为undefined (-- ::)转载▼ 标签: js jquery checkbox checked undefined 分类: JQuery 使 ...

  9. 34、Shiro框架入门三,角色管理

    //首先这里是java代码,就是根据shiro-role.ini配置文件中的信息来得到role与用户信息的对应关系//从而来管理rolepublic class TestShiroRoleTest e ...

  10. 【转载】Java常量池

    本篇随笔为转载,原贴地址:Java常量池理解与总结. (其实Java的常量池有点像C++中的存储字符串常量的常量存储区). 一.相关概念 什么是常量用final修饰的成员变量表示常量,值一旦给定就无法 ...