分析:

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

    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. [Tex学习笔记]发一篇文章的经历

    打算在 INTERNATIONAL JOURNAL OFCONTEMPORARY MATHEMATICAL SCIENCES 发一篇文章, 所以就直接在 作者指引中下载 tex 模版, 写好后发邮件到 ...

  2. Oracle游标示例

    -- 声明游标:CURSOR cursor_name IS select_statement --For 循环游标 --(1)定义游标 --(2)定义游标变量 --(3)使用for循环来使用这个游标 ...

  3. [ActionScript 3.0] 喷泉效果

    pall为水珠影片剪辑 var count:int = 500; var zl:Number = 0.5; var balls:Array; balls = new Array(); for (var ...

  4. delphi中Message消息的使用方法

    实例1 unit Unit1; interface   uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls ...

  5. [DFNews] GetData也出取证软件了

    从事计算机取证的应该都听说过MIP(Mount Image Pro).VFC仿真和Recover My Files,上述三个应用比较广泛的软件都是GetData公司的产品.GetData现在也推出了自 ...

  6. IQ推理:P先生和Q先生

    P先生.Q先生具有足够的推理能力.这天,他们正在接受面试. 他们知道桌子的抽屉里有16张扑克牌:红桃 A Q 4黑桃 J 8 4 2 7 3草花 K Q 5 4 6方块 A 5约瀚教授从这16张牌中挑 ...

  7. (Hibernate进阶)Hibernate基本原理(一)

    在开始学hibernate之前,一直就有人说:Hibernate并不难,无非是对JDBC进一步封装.一句不难,难道是真的不难还是眼高手低? 如果只是停留在使用的层面上,我相信什么技术都不难,看看别人怎 ...

  8. 使用GDB进行调试

    下面是几篇非常好的GDB使用指南: http://www.cs.cmu.edu/~gilpin/tutorial/ http://oss.org.cn/ossdocs/gnu/linux/gdb.ht ...

  9. android Drawable的问题

    1.资源解析成Drawable getDrawable(int id); 挺简单一方法,可是 require api 21......如何向下兼容呢???? 幸亏有ContextCompat类...( ...

  10. java中常用数据类型转换器

    /** * 把String转换成long * * @param src 要转换的String * @param def 转换失败时返回此值 * @return 转换好的long */ public s ...