(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[] ...
随机推荐
- Aooms_微服务基础开发平台实战_003_配置文件与简单的web环境搭建
一.前言 本篇文章介绍两个重点 (1) 工程核心配置文件application.yml (2) 如何在一个标准的的SpringCloud工程上构建起一个基本的web结构 二.配置文件applicati ...
- 【转】java缩放图片、java裁剪图片代码工具类
一首先看下效果 二工具类 三测试类 在系统的上传图片功能中,我们无法控制用户上传图片的大小,用户可能会上传大到几十M小到1k的的图片,一方面图片太大占据了太多的空间,另一方面,我们没办法在页面上显示统 ...
- .NET读取json数据并绑定到对象
需要引用的命名空间: 读取的具体应用: this代表本实体(对象),通过PopulateObject,直接将读取到的json数据与对象进行绑定 Json保存的具体应用: 将对象保存为Json JObj ...
- JavaScript 基础结构
注释 代码注释可以使用//或者/* */ // 这是一个单行注释 /* * 这是 * 一个 * 多行 * 注释 */ 变量 变量用于存储数据,在同一作用域内变量不得重名,定义语法: ...
- hive 中遇到的正则
1.提取科室中,"科"字前面的内容 regexp_extract(t1.doctor_department_format,'(.*)科') 2.去除字符串中的数字 第一种方式: S ...
- 解决No 'Access-Control-Allow-Origin' header is present on the requested resource.跨域问题(后台(java)解决方法)
附:前端常见跨域解决方案(全) 跨域错误 解决方法 在后台写一个过滤器来改写请求头 附上一个前端不知所以然的后台java代码: public class CorsFilter implements F ...
- 使用addviewController()实现无业务逻辑跳转
需要实现WebMvcConfigurer类,重写addViewControllers方法. 添加@Configuration,等价于xml配置. package dbzx.config; import ...
- openlayer3相关扩展
1 ol3扩展 http://viglino.github.io/ol-ext/ ,里面包含编辑-选择控件,字体,动画,canvas绘制等等实例 2 ol3空间拓扑关系库jsts,有jst衍生过来 h ...
- 小程序实践(十一):showModal的使用
显示模态对话框,确定和取消两个选择+标题+内容的对话框 . // 编辑用户性别 tapSex:function(res){ var that = this wx.showModal({ title: ...
- 解决 winform打开网页 和WebBrowser打开链接360误报拦截的问题
以下方法我已经在自己电脑上验证通过,其他电脑并未测试,请广大读者自行验证并反馈,如果有更好的方法请指教. 在winform中如果使用这种方法弹出网页,例如这样 Process.start(" ...