题目链接: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. Qt实现半透明遮罩效果

    本文索引: 需求 原理 实现遮罩控件 遮罩的使用 需求 我们在显示一些模态对话框的时候,往往需要将对话框的背景颜色调暗以达到突出当前对话框的效果,例如: 对话框的父窗口除了标题栏以外的部分都变暗了,在 ...

  2. .Net语言 APP开发平台——Smobiler学习日志:SmoOne新增考勤功能

    大家好!SmoOne这次新增了考勤功能,大家打开SmoOne应用便可体验,无需重新下载更新.如果没有下载SmoOne客户端,可以在apps.smobiler.com进行下载安装. 另外,SmoOne开 ...

  3. JSON 与 JS 对象的区别与对比

    定义: JSON是什么?JSON是JS的一种简单数据格式,JSON是JavaScript原生格式,它是一种严格的js对象的格式,JSON的属性名必须有双引号,如果值是字符串,也必须是双引号. 问题: ...

  4. Android开发——Notification通知的各种Style详解

    本来是想与之前讲解使用Notification通知使用一起写的,查看了资料,觉得有必要将这Style部分单独拿出来讲解 前篇:Android开发——Notification通知的使用及Notifica ...

  5. JavaScript 代码简洁之道

    摘要: 可以说是<Clean Code>的JS代码示例了,值得参考. 原文:JavaScript 代码简洁之道 作者:缪宇 Fundebug经授权转载,版权归原作者所有. 测试代码质量的唯 ...

  6. angular懒加载

    生成module和routing.module文件 {//路径 path: 'InfectionFillInComponent', loadChildren: './component/his/inf ...

  7. XSS Challenges

    平台: http://www.zixem.altervista.org/XSS/ level1: Payload: http://www.zixem.altervista.org/XSS/1.php? ...

  8. JavaScript函数继承

    在ES6中有了继承,使用extends关键字就能实现.但这里讲的讲的不是这种,而是ES6之前的几种实现继承的方式. (一)原型继承 ECMAScript中将原型链作为实现继承的主要方法.其基本思想是利 ...

  9. linux下磁盘存储空间不足

    把自己平时遇到的问题分享给大家 Question:linux系统Ubuntu下面有一个Trash,当我们删除文件后,清空Trash,过一段时间发现磁盘空间不足 Answer:其实我们并没有真正的删除文 ...

  10. springboot 学习之路 8 (整合websocket(1))

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...