题目链接: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. .Net Excel 导出图表Demo(柱状图,多标签页)

    1 使用插件名称Epplus,多个Sheet页数据应用,Demo为柱状图(Epplus支持多种图表) 2 Epplus 的安装和引用 新建一个工程文件或控制台应用程序 打开 Vs2017 Tools  ...

  2. wpf,visibility属性的多元素绑定及值转换

    visibility实现多元素绑定. 实现多绑定转换 public class VisibilityConverter : IMultiValueConverter { public object C ...

  3. Mysql启动时执行文件init-file的使用

    可以在配置文件里指定mysql启动以后初始执行的SQL文件, 其语法是: 在[mysqld]下指定: init-file="D:/mysql/test.sql",  后面为具体的s ...

  4. Java开发笔记(四十二)日历工具的常见应用

    前面介绍了日历工具Calendar的基本用法,乍看起来Calendar与Date两个半斤八两,似乎没有多大区别,那又何苦庸人自扰鼓捣一个新玩意呢?显然这样小瞧了Calendar,其实它的作用大着呢,接 ...

  5. C#设计模式之二十二备忘录模式(Memento Pattern)【行为型】

    一.引言 今天我们开始讲“行为型”设计模式的第十个模式,该模式是[备忘录模式],英文名称是:Memento Pattern.按老规矩,先从名称上来看看这个模式,个人的最初理解就是对某个对象的状态进行保 ...

  6. CentOS7.3 yum install MySQL5.7

    安装环境:阿里云服务器 + CentOS7.3 测试工具:Navicat for MySQL 参考博客:https://blog.csdn.net/qq_38417808/article/detail ...

  7. javascript常用的41个经典技巧

    1. 将彻底屏蔽鼠标右键 <table border oncontextmenu=return(false)><td>no</table> 可用于Table 2. ...

  8. iOS-UIView指定圆角设置

    圆角设置可以指定左上.左下.右上.右下角:单个指定或多个指定. ///设置圆角[左上.右上角] - (void)setCircular{ UIBezierPath *maskPath = [UIBez ...

  9. C# 一般处理程序ashx接收服务端post过来json数据

    这个和前端js的接收方式有点不一样,前端接收用request.form["xxx"]即可

  10. 结对编程项目——C语言实现WordCount Web化

    结对编程项目 代码地址 201631062219,201631011410 gitee项目地址:https://gitee.com/xxlznb/pair_programming 作业地址:https ...