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 ...
随机推荐
- shell 练习
shell 练习 iii= ] do iii=$[$iii+] echo -n "$iii " done iii= ] do iii=$[$iii+] echo -n " ...
- portal、portlet、portlet容器三个概念
什么是portal Portlet规范中是这样定义portal的: A portal is a web based application that –commonly- provides perso ...
- 百度:在O(1)空间复杂度范围内对一个数组中前后连段有序数组进行归并排序
一.题目理解 题目:数组al[0,mid-1]和al[mid,num-1]是各自有序的,对数组al[0,num-1]的两个子有序段进行merge,得到al[0,num-1]整体有序.要求空间复杂度为O ...
- 1842-A. Broj
#include <iostream> using namespace std; int main() { int n; cin>>n; if(n>0&& ...
- Xplico
http://zhulinu.blog.51cto.com/539189/850909
- 实时时钟、系统时钟和CPU时钟的区别
http://blog.sina.com.cn/s/blog_68f909c30100pli7.html 实时时钟:RTC时钟,用于提供年.月.日.时.分.秒和星期等的实时时间信息,由后备电池供电,当 ...
- FLV封装格式及分析器工具
http://blog.csdn.net/leixiaohua1020/article/details/17934487 FLV封装原理 FLV格式的封装原理,贴上来辅助学习之用. FLV(F ...
- wzplayer for android V1.5.3 (新增YUV文件播放)
wzplayer for android V1.5.3 新增功能 1.使用gl es2 播放 yuv 文件. 联系方式:weinyzhou86@gmail.com QQ:514540005 版权所有, ...
- PowerStack
int curInc; HashMap<Integer, Integer> incMap; Stack<Integer> stack; public SuperStack() ...
- cogs_396_魔术球问题_(最小路径覆盖+二分图匹配,网络流24题#4)
描述 http://cojs.tk/cogs/problem/problem.php?pid=396 连续从1开始编号的球,按照顺寻一个个放在n个柱子上,\(i\)放在\(j\)上面的必要条件是\(i ...