用了《算法笔记》中分块的思路。
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
const int MAXN = 100001;int main(){int N, sz, nbrOfBlock, t, cnt, j;int count[MAXN] = {0};sz = sqrt(MAXN * 1.0);nbrOfBlock = MAXN % sz ? MAXN / sz + 1 : MAXN / sz;std::vector<int> block(nbrOfBlock, 0);std::string str;std::vector<int> vec;std::cin >> N;for(int i = 0; i < N; ++i){std::cin >> str;if(str == "Pop"){if(vec.empty()){printf("Invalid\n");} else{printf("%d\n", vec.back());count[vec.back()]--;block[vec.back() / sz]--;vec.pop_back();}} else if(str == "PeekMedian"){if(vec.empty()){printf("Invalid\n");} else{t = vec.size() % 2 ? (vec.size() + 1) / 2 : vec.size() / 2;cnt = 0;for(j = 0; j < nbrOfBlock; ++j){cnt += block[j];if(cnt >= t){cnt -= block[j];break;}}for(int k = j * sz; k < j * sz + sz; ++k){cnt += count[k];if(cnt >= t){printf("%d\n", k);break;}}}} else{std::cin >> t;++count[t];++block[t / sz];vec.push_back(t);}}return 0;
}
根据树状数组:
#include <cstdio>
#include <iostream>
#include <string>
#include <stack>
#define lowbit(x) ((x) & (-x))
const int MAXN = 100001;int N, t;
int c[MAXN] = {0};
std::string str;
std::stack<int> st;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 PeekMedian(){int sz, left, right, middle;sz = (st.size() + 1) / 2;left = 1;right = MAXN;while(left < right){middle = (left + right) / 2;if(getSum(middle) < sz){left = middle + 1;} else{right = middle;}}printf("%d\n", left);
}int main(){scanf("%d", &N);for(int i = 0; i < N; ++i){std::cin >> str;if(str == "Pop"){if(st.empty()){printf("Invalid\n");} else{t = st.top();st.pop();printf("%d\n", t);update(t, -1);}} else if(str == "Push"){scanf("%d", &t);st.push(t);update(t, 1);} else{if(st.empty()){printf("Invalid\n");} else{PeekMedian();}}}return 0;
}
题目如下:
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