题目地址: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. [BZOJ 3774] 最优选择 【最小割】

    题目链接:BZOJ - 3774 题目分析 此题与“文理分科”那道题目有些类似.都是使用最小割来求解,先加上可能获得的权值,在减掉必须舍弃的权值(最小割). 文理分科是规定每个人和 S 连就是选文,和 ...

  2. Datadog Agent是啥?它消耗什么资源?

    在资本市场不那么喜人的 2015 年融资 9450 万美元的 Datadog,在运维圈刮起了一阵小旋风.作为国外很值得学习的一款平台监控产品,公司人数不足 100 的 Datadog 为什么吸引了投资 ...

  3. 使用div+css制作简单导航 以及要注意问题

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 【HDOJ】3480 Division

    斜率dp+滚动数组. /* 3480 */ #include <iostream> #include <sstream> #include <string> #in ...

  5. 【HDOJ】1253 胜利大逃亡

    经典的BFS,需要注意的是当前时间超过最小时间,输出-1.同时,队列为空时还未返回,证明并未找到终点(可能终点为墙).此时也应该输出-1,这个部分容易wa. #include <cstdio&g ...

  6. 结构体page_cur_t

    /** Type of the index page */ typedef byte page_t; /** Index page cursor */ typedef struct page_cur_ ...

  7. Response.Write用法总结

    问题一: Response.Write 后连接Response.Redirect ,则Response.Write无法显示,直接跳转入Response.Redirect 的页面. 解决方案: Resp ...

  8. Art-Directing SVG图像viewBox属性

    Art-Directing SVG图像viewBox属性 作者:彦子 日期:2015-06-02 点击:992 svg 译者注:根据Google Dev文档的解释,Art Direction在这篇文章 ...

  9. Prism的IEventAggregator事件聚合器, 事件订阅发布, ViewModel之间的通讯

    WPF中时常会遇到ViewModel之间的通讯,ViewModel并不知道自己的View,但是一个View发生的更改需要通知另外一个View. 举一个例子,软件界面上有个人信息,打开一个界面更改用户的 ...

  10. java命名规范和编程技巧

    一个好的java程序首先命名要规范. 命名规范 定义这个规范的目的是让项目中所有的文档都看起来像一个人写的,增加可读性,方便维护等作用 Package 的命名 Package 的名字应该都是由一个小写 ...