不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了

题意:有三种操作,分别是
1.Push key:将key压入stack
2.Pop:将栈顶元素取出栈
3.PeekMedian:返回stack中第(n+1)/2个小的数

建立一个栈来模拟push和pop,另外还需要树状数组,来统计栈中<=某个数的总个数
不了解树状数组的建议学习一下,很有用的。
树状数组为c,有个虚拟的a数组,a[i]表示i出现的次数
sum(i)就是统计a[1]~a[i]的和,即1~i出现的次数
当我要询问第k个数是多少时,那么我可以通过二分查找来找出第k个数
首先另l=min,r=max,这里min=1,max=100000,mid=(l+r)/2
如果k<=sum(mid),说明1~mid的总个数>=k,则第k个数肯定是在1~mid之间,所以r=mid
如果k>sum(mid),说明1~mid的总个数<k,则第k个数肯定是在mid~r之间,所以l=mid
最后到l与r相邻终止循环
此时如果sum(l)<k && k<=sum(r),说明第k个数即为多个r中的一个
否则的话,说明第k个数为多个l中的一个。

当执行push key的时候,update(key,1),即a[key]++,key出现的次数增加1
当执行pop的时候,update(key,-1),即a[key]--,key出现的次数减少1

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int maxn=+;
int n;
int c[maxn]; //树状数组
int lowbit(int x){
return x&(-x);
}
//树状数组的单点更新操作
void update(int i,int val){
while(i<maxn){
c[i]+=val;
i+=lowbit(i);
}
}
//树状数组的区间求和操作
int sum(int i){
int res=;
while(i){
res+=c[i];
i-=lowbit(i);
}
return res;
} int query(int cnt){
//二分查找哪个数是第cnt个大的
int l=,r=maxn-;
while(l<r-){
int mid=(l+r)>>;
//<=mid的数有sum(mid)个
if(cnt<=sum(mid)){
r=mid;
}
else{
l=mid;
}
}
if(sum(l)<cnt && cnt<=sum(r))
return r;
else
return l;
}
void test(){
int sizes=;
int a[]={,,,,,,};
for(int i=;i<sizes;i++){
update(a[i],);
}
int q;
while(scanf("%d",&q)!=EOF){
printf("%d\n",query(q));
}
}
int main()
{
char str[];
memset(c,,sizeof(c));
//test();
scanf("%d",&n);
stack<int>stacks;
int cnt=;
int tmp;
for(int i=;i<n;i++){
scanf("%s",str);
if(str[]=='o'){
if(cnt==)
printf("Invalid\n");
else{
tmp=stacks.top();
stacks.pop();
cnt--;
update(tmp,-);
printf("%d\n",tmp);
}
}
else if(str[]=='u'){
scanf("%d",&tmp);
stacks.push(tmp);
cnt++;
update(tmp,);
//printf("%d\n",tmp);
}
else{
if(cnt==)
printf("Invalid\n");
else{
int idx=(cnt+)/;
int ans=query(idx);
printf("%d\n",ans);
}
}
}
return ;
}

PAT甲级题解-1057. Stack (30)-树状数组的更多相关文章

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

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

  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【树状数组】【二分】

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

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

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

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

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

  6. 【题解】Music Festival(树状数组优化dp)

    [题解]Music Festival(树状数组优化dp) Gym - 101908F 题意:有\(n\)种节目,每种节目有起始时间和结束时间和权值.同一时刻只能看一个节目(边界不算),在所有种类都看过 ...

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

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

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

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

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

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

随机推荐

  1. 各种SQL查询技巧汇总 (转)

    原文地址: https://blog.csdn.net/tim_phper/article/details/54963828 select select * from student; all 查询所 ...

  2. webservice 客户端调用

    /** * 通过webserevice下发工单 * @param url * @param method * @param requestMap * @return * @throws Service ...

  3. css盒子模型(box-sizing)

    盒子模型 关于CSS重要的一个概念就是CSS盒子模型.它控制着页面这些元素的高度和宽度.盒子模型多少会让人产生一些困惑,尤其当涉及到高度和宽度计算的时候.真正盒子的宽度(在页面呈现出来的宽度)和高度, ...

  4. (二) DRF 视图

    DRF中的Request 在Django REST Framework中内置的Request类扩展了Django中的Request类,实现了很多方便的功能--如请求数据解析和认证等. 比如,区别于Dj ...

  5. Android ListView下拉刷新时卡的问题解决小技巧

    问题:ListView下拉刷新时看上去非常的卡 解决方案: 在BaseAdapter的getView方法中,有三个参数 public View getView(int position, View c ...

  6. 添加静态路由 route add -host 子网掩码 -- 在线解析

    1.215        -----       R(172.16.0.1)      <--------- gw(61.146.164.109) |                       ...

  7. Found more than one concrete type for given DbContext Type (xxx.xxxx.xxx) define MultiTenancySideAttribute with Tenant

    错误提示: Found more than one concrete type for given DbContext Type (Abp.Zero.EntityFramework.AbpZeroCo ...

  8. odoo 订单打印 会出现字体. ........... 虚线问题

    在表头加 红色部分 <?xml version="1.0" encoding="utf-8"?><openerp> <data&g ...

  9. C# WPF xml序列化 反序列化

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  10. BYTE数组与16进制字符串互转

    //字节数组转换为HEX 字符串const string Byte2HexString(const unsigned char* input, const int datasize) { ]; ; j ...