题目地址: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. PHP与最丑的后台管理系统

    第二天阿Q到公司还是比较早,同事只有阿梅在,阿Q坐在椅子上旋转来旋转去,有点像个小孩子.公司有书柜,书柜上放了好几本很新的php的书,.net的书反倒比较少而且显得老旧.阿Q起身走过去拿了本php翻了 ...

  2. CSS文档流与块级元素和内联元素(文档)

    CSS文档流与块级元素(block).内联元素(inline),之前翻阅不少书籍,看过不 少文章, 看到所多的是零碎的CSS布局基本知识,比较表面.看过O'Reilly的<CSS权威指 南> ...

  3. BZOJ 3955 Surely You Congest 解题报告

    首先,我们可以求出源为 $1$ 号点的最短路图以及各个点到 $1$ 号点的最短路. 然后我们考虑那些距离不同的点,是一定不会发生拥堵现象的. 然后我们就只需要考虑那些距离相同的点,就相当于做一个最大流 ...

  4. SGU481 Hero of Our Time

    Description Saratov ACM ICPC teams have a tradition to come together on Halloween and recollect terr ...

  5. List<>过滤重复的简单方法

    List<int> ss = new List<int>(); ss.Add(); ss.Add(); ss.Add(); ss.Add(); ss.Add(); ss.Add ...

  6. Win7下安装Mongodb教程

    最新Mongodb下载地址:http://www.mongodb.org/downloads 1.下载完成后,解压到任意路径,如:D:\mongodb: 2.在D:\mongodb目录下 新建data ...

  7. android一个纠结的VFY错误

    08-16 09:06:45.018: W/dalvikvm(2286): VFY: unable to resolve static method 3273: Lorg/slf4j/LoggerFa ...

  8. Android 自定义title样式

    requestWindowFeature(featrueId),它的功能是启用窗体的扩展特性.参数是Window类中定义的常量.一.枚举常量1.DEFAULT_FEATURES:系统默认状态,一般不需 ...

  9. is present but cannot be translated into a null value due to being declared as a primitive type

    解决办法:把基本类型改为对象,譬如此处将pageId的类型由int 改为Integer 2016-10-19 19:36:11.275 DEBUG [http-nio-9999-exec-2][org ...

  10. rails3 Bundle简介

    Rails 3开始使用bundle来管理项目的gem依赖.该命令只在一个含有Gemfile的目录下执行. Gemfile: Rails 项目所有的依赖包都在这里配置,不像以前通过require来查找 ...