1057 Stack (30 分)

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 linecontains a positive integer N (≤10​5​​ ). 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 10​5
​​ .

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

树状数组:

#include <bits/stdc++.h>
using namespace std;
#define mod 100005
int n;
int m;
string s;
deque<int> q;
int tree[mod];

int lowbit(int x){
    return x&(-x);
}

void add(int pox, int val){
    while(pox < mod){
        tree[pox] += val;
        pox += lowbit(pox);
    }
}

int getsum(int x){
    int sum = 0;
    while(x > 0){
        sum += tree[x];
        x -= lowbit(x);
    }
    return sum;
}

int binary_search(int val){
    val = (val+1)>>1;
    int l = 1, r = 100000, mid;
    while(l < r){
        mid = (l+r)>>1;
        if(getsum(mid) < val){
            l = mid + 1;
        }else{
            r = mid;
        }
    }
    return r;
}


int main(){
    // cin >> n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        cin >> s;
        if(s=="Pop"){
            if(!q.empty()){
                printf("%d\n", q.front());
                add(q.front(), -1);
                q.pop_front();
            }else{
                printf("Invalid\n");
            }
        }else if(s=="PeekMedian"){
            if(!q.empty()){
                int len = q.size();
                int it = binary_search(len);
                printf("%d\n", it);
            }else{
                printf("Invalid\n");
            }
        }else{
            // cin >> m;
            scanf("%d", &m);
            q.push_front(m);
            add(m,1);
        }
    }
    return 0;
}

multiset做法:

// stack
// 查找一般要用二分查找 ,分析如果题中出现求明确位置的元素,则需要用一个数据结构直接进行维护最快
// multiset,与set的区别是可以出现重复的元素,且一次插入和删除的时间都可以在lgn完成 \
// 注意erase的删除操作如果是下标则删除下标,如果是元素,只要与他想等的都会被删除 
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;

vector<int> sta;
multiset<int> s1; //将小于等于mid(这个值可以任意改变)的进入第一个multiset,将大于的放入第二个
multiset<int> s2;
int n;
char f[20];
int mid;
int val;
void adjust() //调整
{
	multiset<int>:: iterator it;
	if(s1.size()<s2.size())
	{
		it = s2.begin();
		s1.insert(*it);
		s2.erase(it);
	}
	if(s1.size()>s2.size()+1)
	{
		it = s1.end();
		it--;
		s2.insert(*it);
		s1.erase(it);
	}
	if(!s1.empty())
	{
		it = s1.end();
		it--;
		mid = *it;
	}
 } 
int main()
{
	cin>>n;
	for(int i = 0;i<n;i++)
	{
		scanf("%s",&f);
		if(f[1]=='u')
		{
			getchar();
			scanf("%d",&val);
			sta.push_back(val); //入栈
			if(s1.empty()) s1.insert(val);
			else if(val<=mid) s1.insert(val);
			else s2.insert(val);
			adjust();
		}
		else if(f[1]=='o')
		{
			if(sta.size()==0)
				printf("Invalid\n");
			else{
				int a = sta[sta.size()-1];
				printf("%d\n",a);
				sta.pop_back();
				if(a<=mid) s1.erase(s1.find(a));
				else s2.erase(s2.find(a));
				adjust();
			}
		}
		else
		{
			if(sta.size()==0)
				printf("Invalid\n");
			else
			{
				printf("%d\n",mid);
			}
		}
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值