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 line contains 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

思路:

数字范围是1e5,那么我们可以用一个数组记录每个数字的个数,而找中间值,我们可以对记录个数的数组的前缀和进行二分。

而每次插入都求一遍前缀和显然不可接受,所以这个地方需要用树状数组(O(logN)插入 O(LogN)查询)来维护前缀和。

AC代码:

//#include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <string.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
//#define mp Debug(x) printf("%d\n", &x);
#define pb push_back
#define ms(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1046513837;
const int maxn = 1e5 + 50;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
//typedef vector<ll> vec;
//typedef vector<vec> mat;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
int pre[maxn];
int cnt[maxn];
vector<int> vec;
int lowbit(int x){
    return x & (-x);
}
void add(int pos, int val){
    while(pos < maxn){
        pre[pos]+= val;
        pos += lowbit(pos);
    }
}
int query(int pos){
    int res = 0;
    while(pos){
        res += pre[pos];
        pos -= lowbit(pos);
    }
    return res;
}
int main() {
    int n;
    cin >> n;
    char cmd[20];
//    rep(i, 1, 100){
//        add(i, i);
//    }
//    cout << query(5) <<endl;
//    cout << query(100);
    int tmp;
    while(n--){
        scanf("%s", cmd);
        if(cmd[1] == 'o'){
            if(vec.size() == 0){
                printf("Invalid\n");
            }else{
                printf("%d\n", (vec.back()));
                add(vec.back(), -1);
                vec.pop_back();
            }
        }else if(cmd[1] == 'u'){
            scanf("%d", &tmp);
            add(tmp, 1);
            vec.push_back(tmp);
        }else{
            if(vec.size() == 0){
                printf("Invalid\n");
                continue;
            }
            int l = 0, r = 1e5+10;
            int fd = (vec.size() + 1) / 2;
            while(l < r){
                int mid = (l+r)/2;
                if(query(mid) < fd){
                    l = mid +1;
                }else{
                    r = mid;
                }
            }
            printf("%d\n", l);
        }
    }
	return 0;
}
/*
110
010


*/
发布了276 篇原创文章 · 获赞 21 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_40758751/article/details/103430717