PAT 1057. Stack (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1057
用树状数组和二分搜索解决,对于这种对时间复杂度要求高的题目,用C的输入输出显然更好
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
using namespace std; const int NUM=; struct TreeArray
{
vector<int> cStore;
TreeArray()
{
cStore=vector<int>(NUM,);
}
int lowerBit(int x)
{
return x&(-x);
}
void add(int location,int addedValue)
{
while(location<=NUM)
{
cStore[location]+=addedValue;
location+=lowerBit(location);
}
}
int getSum(int location)
{
int sum();
while(location>)
{
sum+=cStore[location];
location-=lowerBit(location);
}
return sum;
} int findMedian(int toFind,int low=,int high=NUM)
{
if(low==high)
return low;
int median=(low+high)>>;
if(getSum(median)<toFind)
{
return findMedian(toFind,median+,high);
}
else
{
return findMedian(toFind,low,median);
}
}
}; TreeArray treeArray;
stack<int> s; int _tmain(int argc, _TCHAR* argv[])
{
int N;
scanf("%d",&N);
char command[];
int key;
for(int i=;i<N;++i)
{
scanf("%s",command);
switch(command[])
{
case 'u':
scanf("%d",&key);
s.push(key);
treeArray.add(key,);
break;
case 'e':
if(s.empty())
printf("Invalid\n");
else
{
printf("%d\n",treeArray.findMedian((s.size()+)/));
}
break;
case 'o':
if(s.empty())
printf("Invalid\n");
else
{
key=s.top();
s.pop();
printf("%d\n",key);
treeArray.add(key,-);
break;
}
}
}
return ;
}
PAT 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 [难][树状数组]
1057 Stack (30)(30 分) Stack is one of the most fundamental data structures, which is based on the pr ...
- pat 甲级 1057 Stack(30) (树状数组+二分)
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...
- PAT (Advanced Level) 1057. Stack (30)
树状数组+二分. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...
- PAT甲级题解-1057. Stack (30)-树状数组
不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekM ...
- 【PAT甲级】1057 Stack (30 分)(分块)
题意: 输入一个正整数N(<=1e5),接着输入N行字符串,模拟栈的操作,非入栈操作时输出中位数.(总数为偶数时输入偏小的) trick: 分块操作节约时间 AAAAAccepted code: ...
- 1057. Stack (30)
分析: 考察树状数组 + 二分, 注意以下几点: 1.题目除了正常的进栈和出栈操作外增加了获取中位数的操作, 获取中位数,我们有以下方法: (1):每次全部退栈,进行排序,太浪费时间,不可取. (2) ...
- 1057. Stack (30) - 树状数组
题目如下: Stack is one of the most fundamental data structures, which is based on the principle of Last ...
- 1057 Stack (30)(30 分)
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
随机推荐
- servlet向ajax传递list数据类型,ajax向servlet传递array数据类型
因工作需要, 1,后台向前台传递一个list 2,前台向后台传递类似于list的结构,但是因为javascript不支持list类型,所以只能使用二维数组代替 后台运行后的截图: ...
- 函数page_rec_get_next_const
/************************************************************//** Gets the pointer to the next recor ...
- WebChart网页局域网聊天系列(一):ActiveX插件编写
第一步:创建ActiveX控件类库,在解决方案中右击添加Window窗体控件库 在该类库属性中,设置 使程序集COM可见,同时设置为COM互操作注册 另外在自动生成的文件中AssemblyInfo.c ...
- 转自 void- man 差分约束系统详解
一直不知道差分约束是什么类型题目,最近在写最短路问题就顺带看了下,原来就是给出一些形如x-y<=b不等式的约束,问你是否满足有解的问题 好神奇的是这类问题竟然可以转换成图论里的最短路径问题,下面 ...
- matlab添加M_map工具箱(转 http://blog.sina.com.cn/s/blog_491b86bf0100srt9.html)
之前转载过matlab画世界地图的博文.最近正好用到.首先试了matlab自带的worldmap,感觉画出来的图形不尽如人意,比较杂乱.如下图. 略查阅了些资料,请教了Liangjing,一致推荐m_ ...
- 利用URLRewriter重写url地址
首先,当然是下载URLRewriter了 download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDN ...
- diamond专题(三)—— diamond架构
大家好,这次为大家带来的是diamond的架构,架构如下图所示: 对该图进行一些说明: 1.作为一个配置中心,diamond的功能分为发布和订阅两部分.因为diamond存放的是持久数据,这些数据的变 ...
- [King.yue]Ext.JS 弹出窗体取值赋值
//从Grid取值 var name = Ext.getCmp(gridGridID).getView().getSelectionModel().getSelection()[0].data.Nam ...
- .net软件自动化测试笔记(API-1)
.net 软件测试自动化之道 API(Application Programming Interface)包括单元测试(Unit Testing),模块测试(Module Testing),组件测试( ...
- CentOS+Nginx一步一步开始配置负载均衡
Nginx负载均衡的理解 http://www.linuxdiyf.com/linux/10205.html Nginx是一个轻量级的.高性能的WebServer,他主要可以干下面两件事: 作为htt ...