一、栈/队列

栈模拟、括号匹配,单调栈

noip:双栈排序

 

二、并查集

注意fa[]数组的初始值和路径压缩

Noip:关押罪犯

 

三、堆

 noip:合并果子

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 1000009
using namespace std;

int n,tot;

int d[N];

void up(int x){
    if(x==0)return;
    if(d[x]<d[x/2]){
        swap(d[x],d[x/2]);
        up(x/2);
    }
}

void down(int x){
    int nxt;
    if(x*2>tot)return;
    if(x*2+1>tot)nxt=x*2;
    else nxt=d[x*2]<d[x*2+1]?x*2:x*2+1;
    if(d[x]>d[nxt]){
        swap(d[x],d[nxt]);
        down(nxt);
    }
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int od;
        scanf("%d",&od);
        if(od==1){
            int x;
            scanf("%d",&x);
            d[++tot]=x;
            up(tot);
        }
        if(od==2) printf("%d\n",d[1]);
        if(od==3){
            d[1]=d[tot];
            tot--;
            down(1);
        }
    }
    return 0;
}
洛谷模板

相关文章:

  • 2022-01-08
  • 2022-01-02
  • 2022-01-03
  • 2021-09-06
  • 2022-12-23
  • 2022-01-08
  • 2021-09-15
  • 2021-08-04
猜你喜欢
  • 2022-01-08
  • 2021-08-30
  • 2022-12-23
  • 2021-11-28
  • 2022-01-08
相关资源
相似解决方案