题目链接:https://www.patest.cn/contests/gplt/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(<= )和M(<= ),分别是插入元素的个数、以及需要判断的命题数。下一行给出区间[-, ]内的N个要被插入一个初始为空的小顶堆的整数。之后M行,每行给出一个命题。题目保证命题中的结点键值都是存在的。 输出格式: 对输入的每个命题,如果其为真,则在一行中输出“T”,否则输出“F”。
输入样例: is the root
and are siblings
is the parent of
is a child of 输出样例: F
T
F
T

题目大意:把N个数插入一个初始化为空的小顶堆里,判断下面M个语句的对错

方法:由这N个数以插入的方法构建最小堆,再判断

注:最小堆是任意根节点比左后节点都小的一种二叉树

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<math.h>
#include<map>
#include<vector>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a))
#define N 5006
int tree[N];
int n;
void buile(int s)///插入一个节点与它的根节点比较
{
if(s==)
return ;
while(s!=)
{
if(tree[s]<tree[s/])///如果比根节点小,与根节点交换
{
swap(tree[s],tree[s/]);
s=s/;
}
else
break;
}
}
void root(char str[])
{
int x=;
int l=strlen(str),i;
if(str[]=='-')
i=;
else
i=;
for(; str[i]!=' '; i++)
{
x=x*+str[i]-'';
}
if(str[]=='-')
x=x*-;
if(tree[]!=x)
printf("F\n");
else
printf("T\n");
}
void sib(char str[])
{
int x=,y=;
int xx=,yy=,i,j;
if(str[]=='-')
i=;
else
i=;
for(; str[i]!=' '; i++)
x=x*+str[i]-'';
j=i+;
if(str[j]=='-')
j++;
for(; str[j]!=' '; j++)
y=y*+str[j]-'';
if(str[]=='-')
x*=-;
if(str[i+]=='-')
y*=-;
for(i=; i<=n; i++)
{
if(tree[i]==x)
xx=i;
if(tree[i]==y)
yy=i;
}
if((xx/)!=(yy/))
printf("F\n");
else
printf("T\n");
}
void par(char str[])
{
int x=,y=,i,xx=,yy=,k;
int l=strlen(str);
if(str[]=='-')
i=;
else
i=;
for(; str[i]!=' '; i++)
x=x*+str[i]-'';
if(str[]=='-')
x*=-;
int j=l-;
while(str[j]!=' ') j--;
k=++j;
if(str[j]=='-')
j++;
for(; j<l; j++)
{
y=y*+str[j]-'';
}
if(str[k]=='-')
y*=-;
for(int i=; i<=n; i++)
{
if(tree[i]==x)
xx=i;
if(tree[i]==y)
yy=i;
}
if((xx*==yy) || ((xx*+)==yy))
printf("T\n");
else
printf("F\n");
}
void child(char str[])
{
int x=,y=,xx,yy,i,k;
int l=strlen(str);
if(str[]=='-')
i=;
else
i=;
for(; str[i]!=' '; i++)
x=x*+str[i]-'';
if(str[]=='-')
x*=-;
int j=l-;
while(str[j]!=' ') j--;
k=++j;
if(str[j]=='-')
j++;
for(; j<l; j++)
{
y=y*+str[j]-'';
}
if(str[k]=='-')
y*=-;
for(int i=; i<=n; i++)
{
if(tree[i]==x)
xx=i;
if(tree[i]==y)
yy=i;
}
if(xx/==yy)
printf("T\n");
else
printf("F\n");
}
int main()
{
int m,x,y;
char str[];
scanf("%d %d",&n,&m);
for(int i=; i<=n; i++)
{
scanf("%d",&tree[i]);
buile(i);
}
getchar();
while(m--)
{
x=;
y=;
//getchar();
gets(str);
if(strstr(str,"is the root")!=NULL)
root(str);
else if(strstr(str,"are siblings")!=NULL)
sib(str);
else if(strstr(str,"is the parent of")!=NULL)
par(str);
else
child(str); }
return ;
}

还有一种建立最小堆的方法,已知一个序列建立最小堆

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<math.h>
#include<map>
#include<vector>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a))
#define N 5006
int tree[N];
int n;
void buile(int l,int r)
{
int team=tree[l];
for(.int i=l*;i<=r;i*=)
{
if(tree[i]>tree[i+])
i++;
if(tree[i]>team)
break;
tree[l]=tree[i];
l=i;
}
tree[s]=team;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&tree[i]);
}
for(int i=n/;i>=;i++)
{
buile(i,n);
}
for(int i=;i<=n;i++)
printf("%d%c",tree[i],i==n?'\n':' ');
return ;
}

(PAT)L2-012 关于堆的判断 (最小堆)的更多相关文章

  1. ->code vs 2879 堆的判断(堆的学习一)

    2879 堆的判断  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold   题目描述 Description 堆是一种常用的数据结构.二叉堆是一个特殊的二叉树,他的父 ...

  2. My集合框架第五弹 最小堆

    二叉堆(以最小堆为例),其具有结构性质和堆序性质结构性质: 堆是一棵完全的二叉树,一颗高为h的完全二叉树有2^h到2^h-1个节点,高度为log N            而且该结构可以很容易的使用数 ...

  3. C语言实现哈夫曼编码(最小堆,二叉树)

    // 文件中有通过QT实现的界面#include <stdio.h> #include <stdlib.h> #include <string.h> typedef ...

  4. java实现最小堆

    1.堆:通常通过二叉堆,实为二叉树的一种,分为最小堆和最大堆,具有以下性质: 任意节点小于它的所有后裔,最小元在堆的根上. 堆总是一棵完全树 将根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做最小 ...

  5. 最小堆的两种实现及其STL代码

    #include<cstdio> #include<iostream> #include<algorithm> #include<vector> boo ...

  6. libevent中最小堆实现算法解析

    libevent,一个非常好的c的网络库,最近开始学习并分析下,做个记录.源码选用的1.4版本.因为感觉这版的代码比较精简,也没有太多宏定义,个人感觉适合学习原理. 从哪里开始呢,我选择从一些最简单的 ...

  7. PAT L2-012 关于堆的判断

    https://pintia.cn/problem-sets/994805046380707840/problems/994805064676261888 将一系列给定数字顺序插入一个初始为空的小顶堆 ...

  8. PAT-1147(Heaps)最大堆和最小堆的判断+构建树

    Heaps PAT-1147 #include<iostream> #include<cstring> #include<string> #include<a ...

  9. pat 团体天梯赛 L2-012. 关于堆的判断

    L2-012. 关于堆的判断 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的小顶堆H[] ...

随机推荐

  1. 第33章 密码学(Cryptography),密钥(Keys)和HTTPS - Identity Server 4 中文文档(v1.0.0)

    IdentityServer依赖于几个加密机制来完成它的工作. 33.1 令牌签名和验证 IdentityServer需要非对称密钥对来签署和验证JWT.此密钥对可以是证书/私钥组合或原始RSA密钥. ...

  2. Java开发笔记(十七)各得其所的多路分支

    前面提到条件语句的标准格式为“if (条件) { /* 条件成立时的操作代码 */ } else { /* 条件不成立时的操作代码 */ }”,乍看之下仿佛只有两个分支,一个是条件成立时的分支,另一个 ...

  3. Windows系统 应用或游戏 打开出现0xc000007b错误 解决方法

    1.使用directX修复工具(推荐) 标准版 增强版 标准版备用地址 增强版备用地址 2. 重新安装DirectX 9.0 安装包(安装包体积大) 微软官方离线安装包 摘录CSDN博客 运行游戏时出 ...

  4. java8 Stream操作

    Stream操作详解:https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/#icomments

  5. 手机端input[type=date]的placeholder不起作用

    <div class="input clearfix"> <label class="fl">起始日期</label> &l ...

  6. SAP MM MIGO & Return Delivery 组合实现部分数量的Reversal

    SAP MM MIGO & Return Delivery 组合实现部分数量的Reversal 在笔者看来,MIGO这个事务代码里的Return Delivery主要用于采购退货场景. 先找到 ...

  7. Rxjs常用operators

    本文使用的是angular6内置的rxjs,版本号为6.3.3 concat 通过顺序地发出多个 Observables 的值将它们连接起来,一个接一个的. 参数: 名称 类型 属性 描述 other ...

  8. windows 服务中托管asp.net core

    在windows 服务中托管asp.net core SDK 2.1.300 官方示例 1.添加运行标识符 xml <PropertyGroup> <TargetFramework& ...

  9. SQL SERVER 执行动态SQL EXEC

    :普通SQL语句可以用Exec执行 eg: Select * from tableName Exec('select * from tableName') Exec sp_executesql N's ...

  10. MySQL常用字符串函数

    字符串函数 是最常用的的一种函数,在一个具体应用中通常会综合几个甚至几类函数来实现相应的应用: 1.LOWER(column|str):将字符串参数值转换为全小写字母后返回 mysql> sel ...