2 条题解

  • 0
    @ 2023-11-11 13:48:58

    优先队列水过:

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
        int n;
        priority_queue<int,vector<int>,greater<int> > q;
        cin>>n;
        for(int i=1;i<=n;i++){
            int op,x;
            cin>>op;
            if(op==1){
                cin>>x;
                q.push(x);
            }
            if(op==2){
                cout<<q.top()<<endl;
            }
            if(op==3){
                q.pop();
            }
        }
    }
    
    • 0
      @ 2023-9-28 17:51:26

      手写堆:

      #include<bits/stdc++.h>
      using namespace std;
      const int N=1e6+5;
      int len,heap[N];
      void push(int x){
          heap[++len]=x;
          int i=len;
          while(i>1&&heap[i]<heap[i/2]){
              swap(heap[i],heap[i/2]);
              i=i/2;
          }
      }
      void pop(){
          heap[1]=heap[len--];
          int i=1;
          while(2*i<=len){
              int son=2*i;
              if(son<len&&heap[son+1]<heap[son]){
                  son++;
              }
              if(heap[son]<heap[i]){
                  swap(heap[son],heap[i]);
                  i=son;
              }
              else{
                  break;
              }
          }
      }
      int main(){
          int n;
          cin>>n;
          while(n--){
              int x,y;
              cin>>x;
              if(x==1){
                  cin>>y;
                  push(y);
              }
              if(x==2){
                  cout<<heap[1]<<endl;
              }
              if(x==3){
                  pop();
              }
          }
          return 0;
      }
      

      STL

      #include<bits/stdc++.h>
      #include <queue>
      using namespace std;
      priority_queue<int,vector<int>,greater<int> > q;
      int n,x,y;
      int main(){
          cin>>n;
          while(n--){
              cin>>x;
              if(x==1){
                  cin>>y;
                  q.push(y);
              }
              if(x==2){
                  cout<<q.top()<<endl;
              }
              if(x==3){
                  q.pop();
              }
          }
          return 0;
      }
      
      • 1

      信息

      ID
      2314
      时间
      1000ms
      内存
      512MiB
      难度
      2
      标签
      递交数
      23
      已通过
      15
      上传者