• 7-12 关于堆的判断 (25分)


    将一系列给定数字顺序插入一个初始为空的小顶堆H[]。随后判断一系列相关命题是否为真。命题分下列几种:

    • x is the rootx是根结点;
    • x and y are siblingsxy是兄弟结点;
    • x is the parent of yxy的父结点;
    • x is a child of yxy的一个子结点。

    输入格式:

    每组测试第1行包含2个正整数N≤ 1000)和M≤ 20),分别是插入元素的个数、以及需要判断的命题数。下一行给出区间[内的N个要被插入一个初始为空的小顶堆的整数。之后M行,每行给出一个命题。题目保证命题中的结点键值都是存在的。

    输出格式:

    对输入的每个命题,如果其为真,则在一行中输出T,否则输出F

    输入样例:

    5 4
    46 23 26 24 10
    24 is the root
    26 and 23 are siblings
    46 is the parent of 23
    23 is a child of 10
    
     

    输出样例:

    F
    T
    F
    T

    大顶堆:每个结点的值都大于或等于其左右孩子结点的值

    小顶堆:每个结点的值都小于或等于其左右孩子结点的值

    堆函数的四个主要用法:

    1.make_heap(first,last,compare_function)范围--[first,last)

     若构建数组0-n的小顶堆,则make_heap(a,a+n,greater<int>());

    2.pop_heap(first,last,compare_function) 

     不是真的把最大(最小)的元素从堆中弹出来。而是重新排序堆。它
    把first和last交换,然后将[first,last-1)的数据再做成一个堆。

    3.push_heap(first,last,compare_function) 

     push_heap()假设由[first,last-1)是一个有效的堆,然后,再把堆中的新元素加
    进来,做成一个堆。-----对于此题来讲用push_heap和make_heap没什么区别,因为此题要求按序插入

    #include<bits/stdc++.h>
    using namespace std;
    int a[1005],n,m;
    int findd(int x){
        int i;
        for(i=0;i<n;i++){
            if(a[i]==x)return i;
        }
    }
    int main(){
        int i;
        cin>>n>>m;
        for(i=0;i<n;i++)cin>>a[i],push_heap(a,a+i+1,greater<int>());
        while(m--){
            char s[10];
            int t;
            scanf("%d %s",&t,s);
            string ss;
            if(s[0]=='a'&&s[1]=='n'&&s[2]=='d'){
                int tt;cin>>tt;
                getline(cin,ss);
                int k1=findd(t),k2=findd(tt);
                if(k2-k1==1&&k2%2==0||k1-k2==1&&k1%2==0)cout<<"T
    ";
                else cout<<"F
    ";
            }
            else{
                getline(cin,ss);
                if(ss.find("root")!=string::npos){
                    if(t==a[0])cout<<"T
    ";
                    else cout<<"F
    ";
                }
                else{
                    for(i=0;i<ss.size();i++){
                        if(ss[i]=='f')break;
                    }
                    string ab="";
                    for(int j=i+2;j<ss.size();j++){
                        ab+=ss[j];
                    }
                    stringstream iii;
                    iii<<ab;
                    int a;iii>>a;
                    if(ss.find("parent")!=string::npos){
                        int k1=findd(t),k2=findd(a);
                        if(k2==k1*2+1||k2==k1*2+2)cout<<"T
    ";
                        else cout<<"F
    ";
                    }
                    else{
                        int k1=findd(t),k2=findd(a);
                        if(k1==k2*2+1||k1==k2*2+2)cout<<"T
    ";
                        else cout<<"F
    ";
                    }
                }
            }
        }
        return 0;
    }
    View Code


    里面有堆排序动画https://zhuanlan.zhihu.com/p/137241931(构造初始堆和调整堆)

  • 相关阅读:
    poj 2109Power of Cryptography
    poj 2632Crashing Robots
    poj 2586Y2K Accounting Bug
    linux0.12中文件系统的一些理解
    latex初学者的经验
    关于linux0.12中的add_entry中bread中的些猜测
    uid gid euid egid详解
    关于linux0.12文件系统目录大小的一个发现
    我的初级muttrc配置
    使用STM32的USB模块中后对USB缓冲区的认识
  • 原文地址:https://www.cnblogs.com/ydw--/p/12582498.html
Copyright © 2020-2023  润新知