1057. Stack (30)

  1. Stack (30)
    时间限制
    100 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

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


本题涉及到分块思想。
将有序序列划分为bnum=sqrt(N)块,然后当和删除存入一个大小为pos的数时,代码如下:

void update(int pos,int choice)
{
    if(choice==1)//新增一个数
    {
        block[pos/bnum]++;
        table[pos]++;
    }
    else//删除一个数
    {
        block[pos/bnum]--;
        table[pos]--;
    }
}

当需要输出时,查找方法如下:

int find(int adr){
    int sum=0,k=0;
    while(sum<adr)//找到所在块
    {
        sum+=block[k++];
    }
    sum-=block[--k];
    int t=k*bnum;
    while(sum<adr){//确定所查找的数字
        sum+=table[t++];
    }
    return t-1;
}

整体代码如下

#include<stdio.h>  
#include<cstring>  
#include<iostream>  
#include<string>  
#include<stack>
#include<algorithm>
using namespace std;
const int N=100005;  
int bnum=sqrt(double(N));
int block[N],table[N];  
void update(int pos,int choice)
{
    if(choice==1)
    {
        block[pos/bnum]++;
        table[pos]++;
    }
    else
    {
        block[pos/bnum]--;
        table[pos]--;
    }
}
int find(int adr){
    int sum=0,k=0;
    while(sum<adr)
    {
        sum+=block[k++];
    }
    sum-=block[--k];
    int t=k*bnum;
    while(sum<adr){
        sum+=table[t++];
    }
    return t-1;

}
int main(){  
    freopen("in.txt","r",stdin);  
    char ss[20];  
    stack<int> Stack;
    int n,pos;  
    memset(block,0,sizeof(block));  
    memset(table,0,sizeof(table));  
    scanf("%d",&n);  
    while(n--){  
        scanf("%s",ss);  
        if(ss[1]=='u'){  
            scanf("%d",&pos);  
            Stack.push(pos);  
            update(pos,1);  
        }else if(ss[1]=='o'){  
            if(Stack.empty()){  
                printf("Invalid\n");  
                continue;  
            }  
            else{ 
                int out=Stack.top();  
                Stack.pop();
                update(out,-1);  
              printf("%d\n",out);  
            }
        }else if(ss[1]=='e'){  
            if(Stack.empty()){  
                printf("Invalid\n");  
                continue;  
            }  
            int len=Stack.size();
            int res;  
            if(len%2==0)  
                res=find(len/2);  
            else  
                res=find((len+1)/2);  
            printf("%d\n",res);  

        }else{  
            printf("Invalid\n");  
        }  
    }  
    return 0;  
}  

本题还有数组数组的解法。代码如下

#include<iostream>
#include<cstring>
#include<algorithm>
#include<stack>
#include<string>
#include<cstdio>
using namespace std;
const int maxn=100010;
#define lowbit(i) ((i)&(-i))
int c[maxn];
void update(int x,int v){
    for(int i=x;i<maxn;i+=lowbit(i)){
        c[i]+=v;
    }
}
int getSum(int x){
    int sum=0;
    for(int i=x;i>0;i-=lowbit(i))
    {
        sum+=c[i];
    }
    return sum;
}
void FindKthElenment(int K){
    int l=1,r=maxn,mid;
    while(l<r){
        mid=(r+l)/2;
        if(getSum(mid)>=K)
        {
            r=mid;
        }
        else l=mid+1;
    }
    printf("%d\n",l);
}
int main(){
    int n,x;
    char ss[12];
    stack <int> Stack;
    scanf("%d",&n);  
    while(n--){  
        scanf("%s",ss);  

        if(ss[1]=='u'){  //将x所在位置+1  
            scanf("%d",&x);  
            Stack.push(x);  
            update(x,1);  
        }else if(ss[1]=='o'){  
            if(Stack.empty()){  
                printf("Invalid\n");  
                continue;  
            }  
            else{ 
                update(Stack.top(),-1);//将栈顶元素所在位置-1  
                printf("%d\n",Stack.top()); 
                Stack.pop();
            }
        }else {
            if(ss[1]=='e'){  
             if(Stack.empty()){  
                  printf("Invalid\n");  
                 continue;  
                }  
             FindKthElenment((Stack.size()+1)/2);  
            }
            else{  
             printf("Invalid\n");  
            }  
        }
    }  
    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值