(PAT)L2-012 关于堆的判断 (最小堆)
题目链接: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 关于堆的判断 (最小堆)的更多相关文章
- ->code vs 2879 堆的判断(堆的学习一)
2879 堆的判断 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 堆是一种常用的数据结构.二叉堆是一个特殊的二叉树,他的父 ...
- My集合框架第五弹 最小堆
二叉堆(以最小堆为例),其具有结构性质和堆序性质结构性质: 堆是一棵完全的二叉树,一颗高为h的完全二叉树有2^h到2^h-1个节点,高度为log N 而且该结构可以很容易的使用数 ...
- C语言实现哈夫曼编码(最小堆,二叉树)
// 文件中有通过QT实现的界面#include <stdio.h> #include <stdlib.h> #include <string.h> typedef ...
- java实现最小堆
1.堆:通常通过二叉堆,实为二叉树的一种,分为最小堆和最大堆,具有以下性质: 任意节点小于它的所有后裔,最小元在堆的根上. 堆总是一棵完全树 将根节点最大的堆叫做最大堆或大根堆,根节点最小的堆叫做最小 ...
- 最小堆的两种实现及其STL代码
#include<cstdio> #include<iostream> #include<algorithm> #include<vector> boo ...
- libevent中最小堆实现算法解析
libevent,一个非常好的c的网络库,最近开始学习并分析下,做个记录.源码选用的1.4版本.因为感觉这版的代码比较精简,也没有太多宏定义,个人感觉适合学习原理. 从哪里开始呢,我选择从一些最简单的 ...
- PAT L2-012 关于堆的判断
https://pintia.cn/problem-sets/994805046380707840/problems/994805064676261888 将一系列给定数字顺序插入一个初始为空的小顶堆 ...
- PAT-1147(Heaps)最大堆和最小堆的判断+构建树
Heaps PAT-1147 #include<iostream> #include<cstring> #include<string> #include<a ...
- pat 团体天梯赛 L2-012. 关于堆的判断
L2-012. 关于堆的判断 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的小顶堆H[] ...
随机推荐
- js密码修改显示与隐藏效果
一.添加input框 <form class="login_form"> <input class="password inputpwd" i ...
- TCP/IP,Web世界的基本规则
TCP/IP协议 TCP/IP 是因特网的通信协议.通信协议是对计算机必须遵守的规则的描述,只有遵守这些规则,计算机之间才能进行通信.浏览器与服务器就是通过这个协议连接在互联网上的,还有电子邮 ...
- C++系列总结——mutable关键字
介绍 mutable的中文意思是易变的,是C++的一个关键字.它的作用就是允许修改被const修饰的对象的成员变量. 常用场景 什么情况下我们会使用到mutable? 一般我们会用const修饰get ...
- Activiti(二) springBoot2集成activiti,集成activiti在线设计器
摘要 本篇随笔主要记录springBoot2集成activiti流程引擎,并且嵌入activiti的在线设计器,可以通过浏览器直接编辑出我们需要的流程,不需要通过eclipse或者IDEA的actiB ...
- Ext中setVersion和getVersion
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- C#监控指定目录的文件变化的代码
如下的资料是关于C#监控指定目录的文件变化的代码. FileSystemWatcher watcher = new FileSystemWatcher();watcher.Path = @" ...
- Android 技能图谱学习路线
这里是在网上找到的一片Android学习路线,希望记录下来供以后学习 1Java 基础 Java Object类方法 HashMap原理,Hash冲突,并发集合,线程安全集合及实现原理 HashMap ...
- 与webview打交道踩过的坑
随着HTML5被越来越多的用到web APP的开发当中,webview这一个神器便日渐凸显出重要地位.简要的说,webview能够在移动应用中开辟出一个窗口,在里面显示html页面,css以及js代码 ...
- mssql sqlserver 指定特定值排在表前面
转自:http://www.maomao365.com/?p=7141 摘要: 下文讲述sql脚本编写中,将 特定值排在最前面的方法分享, 实验环境:sqlserver 2008 R2 例:将数据表中 ...
- Python XML解析之ElementTree
参考网址: http://www.runoob.com/python/python-xml.html https://docs.python.org/2/library/xml.etree.eleme ...