题目如下:

Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). Now you are supposed to implement
a stack with an extra operation: PeekMedian -- return the median value of all the elements in the stack. With N elements, the median value is defined to be the (N/2)-th smallest element if N is even, or ((N+1)/2)-th if N is odd.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<= 105). Then N lines follow, each contains a command in one of the following 3 formats:

Push key

Pop

PeekMedian

where key is a positive integer no more than 105.

Output Specification:

For each Push command, insert key into the stack and output nothing. For each Pop or PeekMedian command, print in a line the corresponding returned value. If the command is invalid, print "Invalid" instead.

Sample Input:

17
Pop
PeekMedian
Push 3
PeekMedian
Push 2
PeekMedian
Push 1
PeekMedian
Pop
Pop
Push 5
Push 4
PeekMedian
Pop
Pop
Pop
Pop

Sample Output:

Invalid
Invalid
3
2
2
1
2
4
4
5
3
Invalid

这个题目我最初用的是string、stringstream和vector来做,发现会严重超时,后来在网上参考了和山米兰的解法,发现他的方法很有技巧,分析如下:

①对命令的解析,只看第二位,如果是o,说明是Pop,如果是e,说明是PeekMedian,否则是push,是push则应当再读入一次数字,这比用getline要好的多,因为getline还需要排除第一个输入的N。

②求中位数的思想,不是排序找中间的值,而是通过统计从1开始的每个元素的个数放到数组C中,这样从前到后,数组C的子列和为题目要求的位置时,拿到的就是中位数。

③求子列和的思想,因为是从前到后的前缀和,可以利用树状数组,下面的代码利用add实现了添加和删除两种操作,利用value的不同,1表示添加,2表示删除。树状数组的基本思想就是数组C中不同元素管辖不同的区域,如果要添加一个元素,则所有满足区域条件的位置都要+value,反之如果删除,所有满足条件的区域都要-value。本题要求的是统计1~100000的元素个数,因此value=+1或者-1。

④求子列和为题目要求的值,利用二分查找。

#include<stdio.h>
#include<cstring>
#include<iostream>
#include<string>
using namespace std; const int N=100001;
int c[N]; int lowbit(int i){
return i&(-i);
} void add(int pos,int value){
while(pos<N){
c[pos]+=value;
pos+=lowbit(pos);
}
} int sum(int pos){
int res=0;
while(pos>0){
res+=c[pos];
pos-=lowbit(pos);
}
return res;
} int find(int value){
int l=0,r=N-1,median,res;
while(l<r-1){
if((l+r)%2==0)
median=(l+r)/2;
else
median=(l+r-1)/2;
res=sum(median);
if(res<value)
l = median;
else
r = median; }
return l+1;
} int main(){
char ss[20];
int stack[N],top=0,n,pos;
memset(c,0,sizeof(c));
scanf("%d",&n);
while(n--){
scanf("%s",ss);
if(ss[1]=='u'){
scanf("%d",&pos);
stack[++top]=pos;
add(pos,1);
}else if(ss[1]=='o'){
if(top==0){
printf("Invalid\n");
continue;
}
int out=stack[top];
add(out,-1); // 删除元素out
printf("%d\n",stack[top--]);
}else if(ss[1]=='e'){
if(top==0){
printf("Invalid\n");
continue;
}
int res;
if(top%2==0)
res=find(top/2);
else
res=find((top+1)/2);
printf("%d\n",res); }else{
printf("Invalid\n");
}
} return 0;
}

1057. Stack (30) - 树状数组的更多相关文章

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

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

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

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

  3. PAT 1057 Stack [难][树状数组]

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

  4. PAT甲级1057 Stack【树状数组】【二分】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和 ...

  5. PAT-1057 Stack (树状数组 + 二分查找)

    1057. Stack Stack is one of the most fundamental data structures, which is based on the principle of ...

  6. PAT1057 Stack(树状数组+倍增)

    目录 题目大意 题目分析 题目大意 要求维护一个栈,提供压栈.弹栈以及求栈内中位数的操作(当栈内元素\(n\)为偶数时,只是求第\(n/2\)个元素而非中间两数的平均值).最多操作100000次,压栈 ...

  7. POJ1990--POJ 1990 MooFest(树状数组)

    Time Limit: 1000MSMemory Limit: 30000K Total Submissions: 8141Accepted: 3674 Description Every year, ...

  8. PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

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

  9. 1057 Stack (30分)(树状数组+二分)

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

随机推荐

  1. cocos2d-x-3.x 学习总结(一)

    这周学习了<cocos2d-x 3.x 游戏开发之旅>的第三章,做如下总结: 1.关于创建标签对象 书中是 Label* label = Label::create(); 可是总是提示出错 ...

  2. 初识 Runtime

    前言 之前在看一些第三方源码的时候,时不时的能碰到一些关于运行时相关的代码.于是乎,就阅读了一些关于运行时的文章,感觉写的都不错,写此篇文章为了记录一下,同时也重新学习一遍. Runtime简介 Ru ...

  3. 分布式锁的几种使用方式(redis、zookeeper、数据库)

    Q:一个业务服务器,一个数据库,操作:查询用户当前余额,扣除当前余额的3%作为手续费 synchronized lock db lock Q:两个业务服务器,一个数据库,操作:查询用户当前余额,扣除当 ...

  4. struts框架从.jsp页面直接访问action

    <%@ page language="java" pageEncoding="UTF-8"%><%String path = request. ...

  5. Java 的异常处理机制

    异常是日常开发中大家都「敬而远之」的一个东西,但实际上几乎每种高级程序设计语言都有自己的异常处理机制,因为无论你是多么厉害的程序员,都不可避免的出错,换句话说:你再牛逼,你也有写出 Bug 的时候. ...

  6. 线性回归(Linear Regression)均方误差损失函数最小化时关于参数theta的解析解的推导(手写)

    第一页纸定义了损失函数的样子, theta, X 和 y 的 shape, 以及最终的损失函数向量表现形式. 第二页纸抄上了几个要用到的矩阵求导公式,以及推导过程和结果. 要说明的是:推导结果与the ...

  7. range和xrange的区别详解

    两种用法介绍如下:1.range([start], stop[, step])返回等差数列.构建等差数列,起点是start,终点是stop,但不包含stop,公差是step.start和step是可选 ...

  8. asp.net使用session完成: 从哪个页面进入登录页面,登录成功还回到那个页面

    1.在Login.aspx页面Load中加入 if (!IsPostBack && Request.UrlReferrer != null) {      Session[ " ...

  9. postgresql 登录查看表定义

    su - postgres psql \connect database_name; \d table_name

  10. Hibernate使用自定义脚本替换注解或者xml文件中的自动生成表结构

    本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/50534361 我们都清楚,可以使用hibernate的metada ...