关于堆的判断

涉及:建小顶堆 (以数组为本,序号从1开始,依据heap[t]<heap[t/2])
堆中数据关系的判断(问题所在:容易多打字符串)

#include<bits/stdc++.h>
using namespace std;
int heap[1010],n;
int find_f(int x)
{
    for(int i=1;i<=n;i++)
        if(heap[i]==x)
            return i;
    return -1;
}
int main()
{
    string tmp;
    int m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>heap[i];
        int t=i;
        while(t>1&&heap[t]<=heap[t/2])
        {
            swap(heap[t],heap[t/2]);
            t/=2;
        }
    }
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>tmp;
        if(tmp=="is")
        {
            cin>>tmp;
            if(tmp=="the")
            {
                cin>>tmp;
                if(tmp=="root")
                {
                    cout<<((heap[1]==x)?"T":"F")<<endl;
                }
                else
                {
                    cin>>tmp>>y;
                    cout<<(heap[find_f(y)/2]==x?"T":"F")<<endl;
                }
            }
            else
            {
                cin>>tmp>>tmp>>y;
                cout<<(heap[find_f(x)/2]==y?"T":"F")<<endl;
            }
        }
        else
        {
            cin>>y;
            getchar();
            getline(cin,tmp);
            int x1=find_f(x);
            int x2=find_f(y);
            cout<<(x1/2==x2/2?"T":"F")<<endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值