zoukankan      html  css  js  c++  java
  • pat 1147 Heaps

    1147 Heaps

    英语

    specialized  adj 专业的
     A common implementation(实施,实现) of a heap is the binary heap
     堆的一个常见实现是二进制堆
    

    思路

    完全二叉树的性质

    ​ 判断是否是大顶堆,小顶堆, 只需要判断父节点和子结点是否满足堆的性质

    需要注意的是 if(-1) 可以输出元素, if(0)此时非

    // 本题主要是 表示if(!) 只有 0 才能表示 if(-1) 同样会输出元素   
    #include<bits/stdc++.h>
    using namespace std;
    const int maxsize = 1005;
    int tree[maxsize], maxHeap = 1, isHeap = 1, cnt = 0, m, n;
    void postOrder(int index) {
        if(index >= n) return;
        postOrder(index * 2 + 1);
        postOrder(index * 2 + 2);
        printf("%s%d", cnt++ == 0 ? "" : " ", tree[index]);
    }
    int main()
    {
        scanf("%d%d", &m, &n);
        while(m--) {
            for(int i = 0; i < n; i++) {
                scanf("%d", &tree[i]);
            }
            isHeap = 1;
            maxHeap = tree[0] >= tree[1] ? 1 : 0; //
            for(int i = 0; i * 2 + 1 < n && isHeap; i++) {
                int left = i * 2 + 1, right = i * 2 + 2;
                if(maxHeap == 1) {
                    if(tree[left] > tree[i]) isHeap = -1;
                    if(right < n && tree[right] > tree[i]) isHeap = -1;
                } else {
                    if(tree[left] < tree[i]) isHeap = -1;
                    if(right < n && tree[right] < tree[i]) isHeap = -1;
                }
            }
            if(isHeap == -1) printf("Not Heap
    ");
            else {
                if(maxHeap) printf("Max Heap
    ");
                else printf("Min Heap
    ");
            }
            postOrder(0);
            cnt = 0;
            printf("
    ");
        }
        return 0;
    }
    
    
  • 相关阅读:
    微信小程序发送模板消息
    Swoole-WebSocket服务端主动推送消息
    git 批量删除分支
    RdKafka使用
    Kakfa安装,PHP安装RdKafka扩展
    Zookeeper安装、启动、启动失败原因
    Hyperf-事件机制+异常处理
    Hyperf-JsonRpc使用
    hyperf-环境搭建
    CGI、FastCGI、PHPFPM
  • 原文地址:https://www.cnblogs.com/csyxdh/p/12422144.html
Copyright © 2011-2022 走看看