zoukankan      html  css  js  c++  java
  • pat 1155 Heap Paths

    pat 1155 Heap Paths

    
    // 本题主要是 表示if(!) 只有 0 才能表示 if(-1) 同样会输出元素
    #include<bits/stdc++.h>
    using namespace std;
    const int maxsize = 1005;
    int n, isHeap = 1, maxHeap = 1;
    vector<int> tree, path, father;
    void getPath(int index, int f) {
        if(index >= n) return;
        if(index * 2 + 1 < n) {
            if(index * 2 + 2 < n) getPath(index * 2 + 2, index);
            getPath(index * 2 + 1, index);
        } else {
            path.push_back(index);
        }
        father[index] = f;
    }
    bool cmp(int a, int b) {
        return a > b;
    }
    int main()
    {
        scanf("%d", &n);
        tree.resize(n), father.resize(n);
        for(int i = 0; i < n; i++) scanf("%d", &tree[i]);
        maxHeap = tree[0] >= tree[1] ? 1 : 0;
        getPath(0, -1);
        for(int i = 0; i < path.size(); i++) {
            //printf("%d", tree[path[i]]);
            vector<int> res, temp;
            int k = path[i];
            temp.push_back(tree[path[i]]);
            while(father[k] != -1) {
                //printf(" %d", tree[father[k]]);
                temp.push_back(tree[father[k]]);
                k = father[k];
            }
            printf("%d", temp[temp.size() - 1]);
            for(auto i = temp.rbegin() + 1; i != temp.rend(); i++) {
                printf(" %d", *i);
            }
            printf("
    ");
            res = temp;
            if(maxHeap) {
                sort(res.begin(), res.end());
            } else
                sort(res.begin(), res.end(), cmp);
            for(int i = 0; i < res.size(); i++) {
                if(res[i] != temp[i]) {
                    isHeap = 0;
                }
            }
        }
        if(isHeap) {
            if(maxHeap)
                printf("Max Heap
    ");
            else
                printf("Min Heap
    ");
        } else {
            printf("Not Heap
    ");
        }
        return 0;
    }
    
    
  • 相关阅读:
    数据结构问题集锦
    大作业 开源项目列表
    数据结构问题集锦
    leetcode174
    leetcode152
    经典算法之KMP
    给出一个字符串,将其每一个字符表示成16进制表示,要求每个十六进制为8位数
    作业
    ASP 作业题
    ASP.NET 作业题
  • 原文地址:https://www.cnblogs.com/csyxdh/p/12422252.html
Copyright © 2011-2022 走看看