首页 > 其他 > 详细

1057 Stack (30 分)

时间:2019-07-15 19:23:22      阅读:126      评论:0      收藏:0      [点我收藏+]
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


这题真心可以,一开始没想那么多,写的有点暴力,结果三组超时,
竟然要用到高级算法,emmmm。。。。这不是超纲了嘛。
虽然对我来说不超纲,但是这对其他人有点难确实。
树状数组可还行,之前想了一下每次都要重新求前缀和,再加上要排序,干脆用个树状数组方便。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define mod 100005
 4 int n;
 5 int m;
 6 string s;
 7 deque<int> q;
 8 int tree[mod];
 9 
10 int lowbit(int x){
11     return x&(-x);
12 }
13 
14 void add(int pox, int val){
15     while(pox < mod){
16         tree[pox] += val;
17         pox += lowbit(pox);
18     }
19 }
20 
21 int getsum(int x){
22     int sum = 0;
23     while(x > 0){
24         sum += tree[x];
25         x -= lowbit(x);
26     }
27     return sum;
28 }
29 
30 int binary_search(int val){
31     val = (val+1)>>1;
32     int l = 1, r = 100000, mid;
33     while(l < r){
34         mid = (l+r)>>1;
35         if(getsum(mid) < val){
36             l = mid + 1;
37         }else{
38             r = mid;
39         }
40     }
41     return r;
42 }
43 
44 
45 int main(){
46     // cin >> n;
47     scanf("%d", &n);
48     for(int i = 0; i < n; i++){
49         cin >> s;
50         if(s=="Pop"){
51             if(!q.empty()){
52                 printf("%d\n", q.front());
53                 add(q.front(), -1);
54                 q.pop_front();
55             }else{
56                 printf("Invalid\n");
57             }
58         }else if(s=="PeekMedian"){
59             if(!q.empty()){
60                 int len = q.size();
61                 int it = binary_search(len);
62                 printf("%d\n", it);
63             }else{
64                 printf("Invalid\n");
65             }
66         }else{
67             // cin >> m;
68             scanf("%d", &m);
69             q.push_front(m);
70             add(m,1);
71         }
72     }
73     return 0;
74 }

 






1057 Stack (30 分)

原文:https://www.cnblogs.com/zllwxm123/p/11190693.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!