A1057 Stack(分块思想和树状数组 第K大)

本文介绍了一种使用分块思想和树状数组实现的算法,该算法能够模拟堆栈操作并实时查询中位数。通过预处理数据,算法在每次push或pop操作后,都能快速定位到中位数,适用于数据流处理和实时数据分析场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

模拟stack的push和pop,并能实时查询中位数。代码是算法笔记中的代码,可作为分块思想和树状数组的模板参考。

分块思想

#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
const int maxn=100010;
const int sqrN=316;

stack<int>st;
int block[sqrN];//表示第i块中存在的元素个数
int table[maxn];//表示整数x的当前存在个数

void peekMedian(int K){
    int sum=0;
    int idx=0;//块号
    while(sum+block[idx]<K){
        sum+=block[idx++];
    }
    int num=idx*sqrN;
    while(sum+table[num]<K){
        sum+=table[num++];
    }
    printf("%d\n",num);
}

void push(int x){
    st.push(x);
    block[x/sqrN]++;
    table[x]++;
}

void pop(){
    int x=st.top();
    st.pop();
    block[x/sqrN]--;
    table[x]--;
    printf("%d\n",x);
}

int main(){
    int x,query;
    memset(block,0,sizeof(block));
    memset(table,0,sizeof(table));
    char cmd[20];
    scanf("%d",&query);
    for(int i=0;i<query;i++){
        scanf("%s",cmd);
        if(strcmp(cmd,"Push")==0){
            scanf("%d",&x);
            push(x);
        }else if(strcmp(cmd,"Pop")==0){
            if(st.empty()==true){
                printf("Invalid\n");
            }else{
                pop();
            }
        }else{
            if(st.empty()==true){
                printf("Invalid\n");
            }else{
                int K=st.size();
                if(K%2==1)
                    K=(K+1)/2;
                else
                    K=K/2;
                peekMedian(K);
            }
        }
    }
    return 0;
}

树状数组

#include <iostream>
#include <stack>
#define lowbit(i) ((i) & (-i))
const int maxn = 100010;
using namespace std;
int c[maxn];
stack<int> s;
void update(int x, int v) {//存储有几个x
    for(int i = x; i < maxn; i += lowbit(i))
        c[i] += v;
}
int getsum(int x) {//求比x小的数有几个
    int sum = 0;
    for(int i = x; i >= 1; i -= lowbit(i))
        sum += c[i];
    return sum;
}
void PeekMedian() {//二分法
    int left = 1, right = maxn, mid, k = (s.size() + 1) / 2;
    while(left < right) {
        mid = (left + right) / 2;
        if(getsum(mid) >= k)
            right = mid;
        else
            left = mid + 1;
    }
    printf("%d\n", left);
}
int main() {
    int n, temp;
    scanf("%d", &n);
    char str[15];
    for(int i = 0; i < n; i++) {
        scanf("%s", str);
        if(str[1] == 'u') {
            scanf("%d", &temp);
            s.push(temp);
            update(temp, 1);
        } else if(str[1] == 'o') {
            if(!s.empty()) {
                update(s.top(), -1);
                printf("%d\n", s.top());
                s.pop();
            } else {
                printf("Invalid\n");
            }
        } else {
            if(!s.empty())
                PeekMedian();
            else
                printf("Invalid\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值