zoukankan      html  css  js  c++  java
  • L2-012. 关于堆的判断

    将一系列给定数字顺序插入一个初始为空的小顶堆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
    

    之前直接排序,肯定不对,老老实实建堆,从下往上建堆发现不对,后来意识到他说的是把输入的按顺序插入一个小顶堆,然后就从上往下建就对了,可以用map存下标,但是我没有用,也对了。

    代码:

    #include <iostream>
    #include <algorithm>
    #include <string>
    using namespace std;
    void siftup(int n,int k,int *p)
    {
        int flag=0;
        while(!flag&&k/2>0)
        {
            if(p[k]<p[k/2])
            {
                swap(p[k],p[k/2]);
                k=k/2;
            }
            else flag=1;
        }
    }
    void Cheap(int *p,int n)
    {
        for(int i=1;i<=n;i++)
            siftup(n,i,p);
    }
    int main()
    {
        int s[1001];
        int n,m;
        int a,b,d;
        string ord;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            cin>>s[i];
        Cheap(s,n);
    
        for(int i=0;i<m;i++)
        {
            cin>>a;
            for(int j=1;j<=n;j++)
            if(s[j]==a){d=j;break;}
            cin>>ord;
            if(ord=="and")
            {
                cin>>b;
                cin>>ord>>ord;
                if(d>1&&d%2&&s[d-1]==b||d<n&&d%2==0&&s[d+1]==b)cout<<'T'<<endl;
                else cout<<'F'<<endl;
            }
            else
            {
                cin>>ord;
                if(ord=="a")
                {
                    cin>>ord>>ord>>b;
                    if(d/2&&s[d/2]==b)cout<<'T'<<endl;
                    else cout<<'F'<<endl;
                }
                else
                {
                    cin>>ord;
                    if(ord=="root")
                    {
                        if(d==1)cout<<'T'<<endl;
                        else cout<<'F'<<endl;
                    }
                    else
                    {
                        cin>>ord>>b;
                        if(d*2<=n&&s[d*2]==b||d*2<n&&s[d*2+1]==b)cout<<'T'<<endl;
                        else cout<<'F'<<endl;
                    }
                }
            }
        }
    }
  • 相关阅读:
    ST_Geometry效率的测试与分析
    ArcEngine中加载ArcGIS Server地图服务
    正则表达式入门教程&&经典Javascript正则表达式(share)
    实现文件上传,以及表单提交成功的回调函数
    Jquery+asp.net实现Ajax方式文件下载实例代码
    Jquery 中 ajaxSubmit使用讲解
    其它课程中的python---4、Matplotlib最最最最简单使用
    其它课程中的python---3、numpy总结(非常全)
    其它课程中的python---2、NumPy模块
    其它课程中的python---1、python基础
  • 原文地址:https://www.cnblogs.com/8023spz/p/7373450.html
Copyright © 2011-2022 走看看