PAT 1057 Stack

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 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 #include<stack>
 5 #define lowbit(x) ((x)&(-x))
 6 using namespace std;
 7 
 8 vector<int> C(100010, 0);
 9  
10 stack<int> s;
11 int getSum(int x){
12   int sum=0;
13   for(int i=x; i>0; i -= lowbit(i))
14     sum += C[i];
15   return sum;
16 }
17 
18 void update(int x, int v){
19   for(int i=x; i<100010; i += lowbit(i))
20     C[i] += v;
21 }
22 
23 void Media(){
24   int l=1, r=100010;
25   int k=(s.size()+1)/2;
26   while(l<r){
27     int mid=(l+r)/2;
28     if(getSum(mid)>=k) r=mid;
29     else if(getSum(mid)<k) l=mid+1;
30   }
31   printf("%d\n", l);
32 }
33 int main(){
34   int n, i;
35   scanf("%d", &n);
36  
37   for(i=0; i<n; i++){
38     char ch[15];
39     scanf("%s", ch);
40     if(ch[1]=='o'){
41       if(!s.size()) printf("Invalid\n");
42       else{
43         printf("%d\n", s.top());
44         update(s.top(), -1);
45         s.pop();
46       }
47     }else if(ch[1]=='u'){
48       int temp;
49       scanf("%d", &temp);
50       s.push(temp);
51       update(temp, 1);
52     }else{
53       if(s.size()) Media();
54       else printf("Invalid\n");
55     }
56   }
57   return 0;
58 }

 

转载于:https://www.cnblogs.com/mr-stn/p/9574616.html

关于 PAT 考试的第 1024 题或相关内容的信息并未在所提供的引用材料中提及。然而,可以从一般的角度探讨 PAT(Programming Ability Test)考试的特点以及其常见题目类型。 PAT 是一项旨在评估编程能力的标准化测试,涵盖了多种计算机科学基础领域的内容,包括但不限于数据结构、算法设计与分析等核心知识点。对于具体编号为 1024 的试题或者关联内容而言,在缺乏直接描述的情况下无法确切给出该题目的解答方案;但是可以根据以往经验推测此类考题往往涉及以下几个方面之一: ### 数据结构的应用 如果此道题目涉及到复杂的数据操作,则很可能考察考生对各种经典数据结构的理解程度及其实际运用技巧。例如链表的操作、树形结构遍历或是图论相关概念等等[^3]。 ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def inorderTraversal(root: TreeNode): res = [] stack = [(root, False)] while stack: node, visited = stack.pop() if not node: continue if visited: res.append(node.val) else: stack.extend([(node.right, False), (node, True), (node.left, False)]) return res ``` 上述代码片段展示了一个二叉树中序遍历实现方式的例子,这可能是某些特定场景下解决问题所需掌握的基础技能之一[^4]。 ### 算法优化策略 另外一方面,考虑到现代互联网服务环境下的动态特性需求日益增长,自适应位率(ABR)技术成为视频流媒体传输中的关键技术点之一。虽然这些并不属于传统意义上的程序设计竞赛范畴之内,但从广义角度来看它们同样体现了如何通过合理选择算法达到性能最优化目标的思想过程[^2]。 综上所述,尽管目前尚无针对PAT Exam Question No.1024的具体说明文档可供查阅,但依据过往积累的知识体系仍可大致勾勒出可能覆盖的主要方向——即围绕高效处理大规模数据集展开讨论并寻求最佳实践路径的过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值