题目地址: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)的更多相关文章

  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 [难][树状数组]

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

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

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

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

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

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

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

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

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

  7. 1057. Stack (30)

    分析: 考察树状数组 + 二分, 注意以下几点: 1.题目除了正常的进栈和出栈操作外增加了获取中位数的操作, 获取中位数,我们有以下方法: (1):每次全部退栈,进行排序,太浪费时间,不可取. (2) ...

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

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

  9. 1057 Stack (30)(30 分)

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

随机推荐

  1. WEB黑客工具箱之FireBug介绍

    Firefox扩展Firebug是一个全功能的Web 应用程序调试器,可以协助Web黑客洞悉复杂的Web 应用程序的内部工作机制.它有两种版本:一种可以跨浏览器使用的组件Firebug Lite,另一 ...

  2. Moloch

    http://www.oschina.net/p/moloch maltego http://www.oschina.net/p/maltego

  3. linux 访问windows共享

    1. windows端建立一个用户user用于共享访问 2. 共享一个目录,设置user可以访问,并在windows系统中确认可以访问 3. linux端创建一个用于挂载共享目录的目录    mkdi ...

  4. HDOJ 4893 Wow! Such Sequence!

    题意是这样的,给定一个n个元素的数组,初始值为0,3种操作: 1 k d将第k个数增加d: 2 l r 询问区间l...r范围内数之和: 3 l r 表示将区间l...r内的数变成离他最近的斐波那契数 ...

  5. 交叉编译 小米路由器mini 的 python(MIPS)

    看了很多文章,要么说的是用opkg安装python,要么说的是小米路由器的交叉编译,就是没有mini的.学习了这篇文章(http://me.deepgully.com/post/56389167868 ...

  6. [OJ] Wildcard Matching (Hard)

    LintCode 192. Wildcard Matching (Hard) LeetCode 44. Wildcard Matching (Hard) 第二次刷还是被这题虐. 其实就是跪在一个地方, ...

  7. Python解决codeforces ---- 1

    第一题 1A A. Theatre Square time limit per test 2 seconds memory limit per test 64 megabytes input stan ...

  8. HTML <!--...--> 注释标签

    HTML <!--...--> 注释标签 实例 HTML 注释: <!--这是一个注释,注释在浏览器中不会显示--> <p>这是一个段落</p>   浏 ...

  9. USACO4.13Fence Loops(无向图最小环)

    最近脑子有点乱 老是不想清楚就啪啪的敲 敲完之后一看 咦..样例都过不去 仔细一想 这样不对啊 刚开始就写了一SPFA 最后发现边跟点的关系没处理好 删了..写dfs..还是没转化好 开始搜解题方法 ...

  10. Java 数组在内存中的结构

    Java中的数组存储两类事物: 原始值(int,char,...),或者引用(对象指针). 当一个对象通过 new 创建,那么将在堆内存中分配一段空间,并且返回其引用(指针). 对于数组,也是同样的方 ...