1057. Stack (30)
分析:
考察树状数组 + 二分, 注意以下几点:
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)的更多相关文章
- PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the prin ...
- pat 甲级 1057 Stack(30) (树状数组+二分)
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...
- PAT 1057. Stack (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1057 用树状数组和二分搜索解决,对于这种对时间复杂度要求高的题目,用C的输入输出显然更好 #i ...
- PAT (Advanced Level) 1057. Stack (30)
树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...
- 1057. Stack (30) - 树状数组
题目如下: Stack is one of the most fundamental data structures, which is based on the principle of Last ...
- PAT甲级题解-1057. Stack (30)-树状数组
不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekM ...
- 1057 Stack (30)(30 分)
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
- 【PAT甲级】1057 Stack (30 分)(分块)
题意: 输入一个正整数N(<=1e5),接着输入N行字符串,模拟栈的操作,非入栈操作时输出中位数.(总数为偶数时输入偏小的) trick: 分块操作节约时间 AAAAAccepted code: ...
- 1057 Stack (30分)(树状数组+二分)
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
随机推荐
- 浅谈可扩展性框架:MEF
之前在使用Prism框架时接触到了可扩展性框架MEF(Managed Extensibility Framework),体验到MEF带来的极大的便利性与可扩展性. 此篇将编写一个可组合的应用程序,帮助 ...
- httpwebrequest详解【转】
http://blog.csdn.net/sjj2011/article/details/7823392 HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最 ...
- math汇总
**** 1/(1^2) + 1/(2^2) + … + 1/(n^2)会收敛到pi^2/6,当n的数位大于6位数字时,最后的结果就是pi^2/6 ****** &的大作用 1.先看看这个n= ...
- 四、maya python plugin
只是作简单的了解. 1区别 (1)Python scripts:可以在Maya的script editor 执行.用于扩展maya.cmd模块. The import statement below ...
- Linux学习笔记——切换并取代用户身份命令——su
再次从头好好的学习Linux,本着以免轻易忘记,以备后用的原则,将我觉得常用或者好玩的linux命令记录在这,注意:我的实验环境是 Ubuntu 14.04.1 su 这个命令我经常使用,因为 ...
- 在配置dubbo框架的时候出现dubbo:application标签无法识别问题。
这原因是因为目前 http://code.alibabatech.com/schema/dubbo/dubbo.xsd标签库打不开.所以我们需要在dubbo的jar包里面引入一个dubbo.xsd 解 ...
- MC的一些具体的应用的例子的总结
任何东西,都有其适用的场景,在合适的场景下,才能发挥好更大的作用. 对于memcached,使用内存来存取数据,一般情况下,速度比直接从数据库和文件系统读取要快的多. memcached的最常用的场景 ...
- Reactjs 入门基础(三)
State 和 Props以下实例演示了如何在应用中组合使用 state 和 props .我们可以在父组件中设置 state, 并通过在子组件上使用 props 将其传递到子组件上.在 render ...
- AIX系统程序异常不释放光驱处理
AIX操作系统有时会出现程序异常不释放光驱,可以用以下命令进行处理: #fuser -kxuc /dev/cd0 或者 #fuser /dev/cd0 以上命令会列出访问光驱设备的所有进程,然后使用k ...
- JStrom的zk数据
/jstorm/masterlock 用于LeaderSelector的锁. /jstorm/master get /jstorm/master localhost.localdomain:7627 ...