zoukankan      html  css  js  c++  java
  • luogu P3378 【模板】堆

    题目描述

    如题,初始小根堆为空,我们需要支持以下3种操作:

    操作1: 1 x 表示将x插入到堆中

    操作2: 2 输出该小根堆内的最小数

    操作3: 3 删除该小根堆内的最小数

    输入输出格式

    输入格式:

    第一行包含一个整数N,表示操作的个数

    接下来N行,每行包含1个或2个正整数,表示三种操作,格式如下:

    操作1: 1 x

    操作2: 2

    操作3: 3

    输出格式:

    包含若干行正整数,每行依次对应一个操作2的结果。

    输入输出样例

    输入样例#1:
    5
    1 2
    1 5
    2
    3
    2
    输出样例#1:
    2
    5

    说明

    时空限制:1000ms,128M

    数据规模:

    对于30%的数据:N<=15

    对于70%的数据:N<=10000

    对于100%的数据:N<=1000000(注意是6个0。。。不过不要害怕,经过编者实测,堆是可以AC的)

    样例说明:

    故输出为2、5

    //堆模板题  STL 
    
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    
    using namespace std;
    
    int heap_size;
    int heap[1000005];
    
    void put(int x) 
    {
        heap[++heap_size]=x;
        push_heap(heap+1,heap+1+heap_size,greater<int>());
    }
    int get() 
    {
        return heap[1];
    }
    void del() 
    {
        pop_heap(heap+1,heap+1+heap_size,greater<int>());
        heap_size--;
    }
    int main() 
    {
        int n;
        scanf("%d",&n);
        int x;
        for(int i=1; i<=n; i++) 
        {
            scanf("%d",&x);
            if(x==1) 
            {
                scanf("%d",&x);
                put(x);
            } 
            else if(x==2) 
            {
                printf("%d
    ",get());
            } 
            else if(x==3) del();
        }
        return 0;
    }
  • 相关阅读:
    texarea动态改变监听
    输入配置
    linux 上安装redis 解压之后使用make命令报错
    sql语句能查询出 放在web程序查询不出
    Eclipse 使用git pull 代码时发生冲突的解决办法
    eclipse git 解决冲突
    微信小程序获取openid
    mybatis 一对多 一对一 xml配置
    Eclipse 中撤消git push提交
    idea 开发中遇到的问题
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/7096308.html
Copyright © 2011-2022 走看看