首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐
2025年6月19日 星期四 7:10am

1057. Stack (30)-PAT甲级真题(树状数组)

  • 24-03-03 14:46
  • 4433
  • 5722
blog.csdn.net

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

题目大意:现请你实现一种特殊的堆栈,它多了一种操作叫“查中值”,即返回堆栈中所有元素的中值。对于N个元素,若N是偶数,则中值定义为第N/2个最小元;若N是奇数,则中值定义为第(N+1)/2个最小元~

分析:用排序查询的方法会超时~用树状数组,即求第k = (s.size() + 1) / 2大的数。查询小于等于x的数的个数是否等于k的时候用二分法更快~

  1. #include
  2. #include
  3. #define lowbit(i) ((i) & (-i))
  4. const int maxn = 100010;
  5. using namespace std;
  6. int c[maxn];
  7. stack<int> s;
  8. void update(int x, int v) {
  9. for(int i = x; i < maxn; i += lowbit(i))
  10. c[i] += v;
  11. }
  12. int getsum(int x) {
  13. int sum = 0;
  14. for(int i = x; i >= 1; i -= lowbit(i))
  15. sum += c[i];
  16. return sum;
  17. }
  18. void PeekMedian() {
  19. int left = 1, right = maxn, mid, k = (s.size() + 1) / 2;
  20. while(left < right) {
  21. mid = (left + right) / 2;
  22. if(getsum(mid) >= k)
  23. right = mid;
  24. else
  25. left = mid + 1;
  26. }
  27. printf("%d\n", left);
  28. }
  29. int main() {
  30. int n, temp;
  31. scanf("%d", &n);
  32. char str[15];
  33. for(int i = 0; i < n; i++) {
  34. scanf("%s", str);
  35. if(str[1] == 'u') {
  36. scanf("%d", &temp);
  37. s.push(temp);
  38. update(temp, 1);
  39. } else if(str[1] == 'o') {
  40. if(!s.empty()) {
  41. update(s.top(), -1);
  42. printf("%d\n", s.top());
  43. s.pop();
  44. } else {
  45. printf("Invalid\n");
  46. }
  47. } else {
  48. if(!s.empty())
  49. PeekMedian();
  50. else
  51. printf("Invalid\n");
  52. }
  53. }
  54. return 0;
  55. }
注:本文转载自blog.csdn.net的柳婼的文章"https://blog.csdn.net/liuchuo/article/details/52231409"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top