PTA-关于堆的判断 (10 分)

1-堆中的路径 (10 分)

将一系列给定数字插入一个初始为空的小顶堆H[]。随后对任意给定的下标i,打印从H[i]到根结点的路径。
输入格式:

每组测试第1行包含2个正整数N和M(≤1000),分别是插入元素的个数、以及需要打印的路径条数。下一行给出区间[-10000,
10000]内的N个要被插入一个初始为空的小顶堆的整数。最后一行给出M个下标。 输出格式:

对输入中给出的每个下标i,在一行中输出从H[i]到根结点的路径上的数据。数字间以1个空格分隔,行末不得有多余空格。

输入样例:

5 3 46 23 26 24 10 5 4 3

输出样例:

24 23 10 46 23 10 26 10

#include<iostream>
#include<map>
using namespace std;
const int maxm=10010;
const int inf=999999999;
int a[maxm],cnt;
void creat(int x)
{
    a[++cnt]=x;
    int t=cnt;
    while(t>1&&(a[t/2]>a[t]))
    {
        a[t]=a[t/2];
        a[t/2]=x;
        t/=2;
    }
    a[t]=x;
}
int main()
{
    int n,m,x,y;
    string s;
    map<int,int>p;
    cin>>n>>m;
    cnt=0;
    a[0]=-10001;
    
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        creat(x);
    }
    for(int i=0;i<m;i++)
    {
        cin>>y;
        int flag=1;
        while(y>0)
        {
            if(flag==1)
            {
                cout<<a[y];
                flag=0;
                y=y/2;
            }
            else {
            cout<<" "<<a[y];
            y=y/2;
            }
        }
        cout<<endl;
    }
}

2-关于堆的判断 (10 分)

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

x is the root:x是根结点;
x and y are siblings:x和y是兄弟结点;
x is the parent of y:x是y的父结点;
x is a child of y:x是y的一个子结点。

输入格式:

每组测试第1行包含2个正整数N(≤ 1000)和M(≤
20),分别是插入元素的个数、以及需要判断的命题数。下一行给出区间[−10000,10000]内的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

思路:

先根据输入数字建造一个小顶堆(即每个父结点要小于两个子结点)

然后利用字符串来判断是哪一个命题,在相应判断就可以了。

#include<bits/stdc++.h>
#include<map>
using namespace std;
const int maxm=10010;
const int inf=999999999;
int a[maxm],cnt;//数组a用来存放堆 cnt计数
void creat(int x)//建造小顶堆
{
    a[++cnt]=x;
    int t=cnt;
    while(t>1&&(a[t/2]>a[t]))//判断是否比父结点(数组下标/2)要小
    {
        a[t]=a[t/2];
        a[t/2]=x;//和父结点相交换
        t/=2;//记录当前下下标位置进入下一层循环
    }
    a[t]=x;
}
int main()
{
    int n,m,x,y;
    string s;
    map<int,int>p;//记录位置
    cin>>n>>m;
    cnt=0;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        creat(x);//创建小顶堆
    }
    for(int i=1;i<=n;i++)
    {
        p[a[i]]=i;//这个p数字就是来记录数组的下标 判断是根结点还是父子结点还是兄弟结点
    }
    //下面运用的就基本上是字符串的知识了
    for(int i=0;i<m;i++)
    {
        cin>>x;
        cin>>s;
        if(s[0]=='a')
        {
            cin>>y;
            getline(cin,s);
            if(p[x]/2==p[y]/2)
            {
                cout<<"T"<<endl;
            }
            else
            {
                cout<<"F"<<endl;
            }
        }
        else
        {
            cin>>s;
            cin>>s;
            if(s[0]=='r')
            {
                if(p[x]==1)
                {
                    cout<<"T"<<endl;
                }
                else
                {
                    cout<<"F"<<endl;
                }
            }
            else if(s[0]=='p')
            {
                cin>>s;
                cin>>y;
                if(p[x]==p[y]/2)cout<<"T"<<endl;
                else cout<<"F"<<endl;
            }
            else
            {
                cin>>s;
                cin>>y;
                if(p[x]/2==p[y])cout<<"T"<<endl;
                else cout<<"F"<<endl;
            }
        }
    }  
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值