zoukankan      html  css  js  c++  java
  • 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
    ", 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
    ");
    42       else{
    43         printf("%d
    ", 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
    ");
    55     }
    56   }
    57   return 0;
    58 }
  • 相关阅读:
    DOS命令行编译运行java
    mysql安装
    ICCV2021 | Vision Transformer中相对位置编码的反思与改进
    ICCV2021 | 医学影像等小数据集的非自然图像领域能否用transformer?
    ICCV2021 | TransFER:使用Transformer学习关系感知的面部表情表征
    2021视频监控中的多目标跟踪综述
    ML2021 | (腾讯)PatrickStar:通过基于块的内存管理实现预训练模型的并行训练
    ICCV2021 | SOTR:使用transformer分割物体
    ICCV2021 | PnPDETR:用Transformer进行高效的视觉分析
    使用Latex/Tex创建自己的简历。
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9574616.html
Copyright © 2011-2022 走看看