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 (-th smallest element if N is even, or (-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 (≤). 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 1.
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
1 /* 2 本题大意:要求模拟一个栈,实现包含压栈和弹栈操作,以及求出栈中元素的中位数的操作。 3 题目显示数据范围1e5, 访问次数1e5,但是如果对栈中元素排序,复杂度为O(n * nlgn)。 4 就会超时,因此我们想到其他方法,我一开始想到的是二叉搜索树,因为二叉搜索树的 5 前序遍历结果就是从小到大,我们找到那个中位数是轻而易举的,且复杂度为O(nlgn),但是 6 写完之后发现超时了,原因应该是树的深度过深了,所以需要平衡搜索数才能ac。 7 这里我说另一种解法。由于是要找一组数中的中位数,而且查询有可能有很多次,因此 8 我们想到,如果用一个数组将前面出现的所有数字出现的次数保存下来,查询中位数 9 就很方便,但是由于这些数字是随着栈的弹出弹入随时变化的,因此我们想到用树状数组 10 或者线段树来维护某一区间内出现数字的个数,刚好数据范围也是1e5,正和题意,那么开搞。 11 */ 12 13 #include <iostream> 14 #include <cstring> 15 #include <string> 16 #include <vector> 17 #define mid ((l + r) >> 1) 18 using namespace std; 19 20 const int maxn = 1e5 + 5; 21 22 int segment_tree[maxn << 2]; 23 24 int n, op; 25 string operation; 26 vector <int> vec; 27 28 string pop = "Pop", push = "Push", median = "PeekMedian"; 29 30 void push_up(int k) { 31 segment_tree[k] = segment_tree[k << 1 | 1] + segment_tree[k << 1]; 32 } 33 34 void update(int k, int l, int r, int num, int c) { 35 if(l == r) { 36 segment_tree[k] += c; 37 return; 38 } 39 if(num <= mid) update(k << 1, l, mid, num, c); 40 else update(k << 1 | 1, mid + 1, r, num, c); 41 push_up(k); 42 } 43 44 int ans; 45 46 int query(int k, int l, int r, int num) { 47 if(l == r) return l; 48 if(segment_tree[k << 1] >= num) return query(k << 1, l, mid, num); 49 else return query(k << 1 | 1, mid + 1, r, num - segment_tree[k << 1]); 50 } 51 52 int main() { 53 cin >> n; 54 while(n --) { 55 cin >> operation; 56 if(operation == push) { 57 cin >> op; 58 update(1, 1, 1e5, op, 1); 59 vec.push_back(op); 60 } else if(operation == pop) { 61 if(vec.size()) { 62 update(1, 1, 1e5, vec.back(), -1); 63 cout << vec.back() << endl; 64 vec.pop_back(); 65 } else cout << "Invalid" << endl; 66 } else if(operation == median) { 67 if(vec.size()) { 68 cout << query(1, 1, 1e5, (vec.size() + 1) / 2) << endl; 69 } else cout << "Invalid" << endl; 70 } 71 } 72 return 0; 73 }