当前位置:网站首页>L2-012 关于堆的判断 (25 分)(堆)
L2-012 关于堆的判断 (25 分)(堆)
2022-08-08 16:51:00 【Here_SDUT】
将一系列给定数字顺序插入一个初始为空的小顶堆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
(≤ 1000)和M
(≤ 20),分别是插入元素的个数、以及需要判断的命题数。下一行给出区间[−10000,10000]内的N个要被插入一个初始为空的小顶堆的整数。之后M
行,每行给出一个命题。题目保证命题中的结点键值都是存在的。
输出格式: 对输入的每个命题,如果其为真,则在一行中输出T,否则输出F。
输入样例:
5 4
46 23 26 24 10
24 is the root
26 and 23 are siblings
46 is the parent of 23
23 is a child of 10
输出样例:
F
T
F
T
代码
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e3+10;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
typedef pair<int,int> PII;
int heap[maxn], n, cnt;
unordered_map<int,int> f;
void up(int x) {
if(x >> 1 >= 1 && heap[x] < heap[x>>1]) {
swap(heap[x],heap[x>>1]);
up(x>>1);
}
}
void down(int x) {
int p = x;
if(x << 1 | 1 <= n && heap[x<<1|1] > heap[p]) p = x<<1|1;
if(x << 1 <= n && heap[x<<1] > heap[x]) p = x << 1;
if(x != p) {
swap(heap[x], heap[p]);
down(p);
}
}
int main(int argc, char const *argv[]) {
int m;
cin >> n >> m;
for(int i = 0; i < n; i++) {
cin >> heap[++cnt];
up(cnt);
}
for(int i = 1; i <= n; i++) {
f[heap[i]] = i;
}
while(m--) {
int x, y;
string s;
cin >> x >>s;
x = f[x];
if(s == "and") {
cin >> y >> s >>s;
y = f[y];
if((x >> 1) == (y >> 1)) puts("T");
else puts("F");
}else {
cin >> s >> s;
if(s == "root") {
if(x == 1) puts("T");
else puts("F");
}
else if(s == "parent") {
cin >> s >> y;
y = f[y];
if((y >> 1) == x) puts("T");
else puts("F");
}
else if(s == "child") {
cin >> s >> y;
y = f[y];
if((x >> 1) == y) puts("T");
else puts("F");
}
}
}
return 0;
}
边栏推荐
- 数字图像处理(六)—— 图像压缩
- 10 Top Open Source Caching Tools for Linux in 2020
- L2-028 秀恩爱分得快 (25 分)
- LeetCode_Backtrack_Medium_491. Incrementing Subsequence
- 二、pytest+selenium+allure实现web ui自动化
- vi编辑器命令
- bzoj1269 [AHOI2006]文本编辑器editor
- C1. Pokémon Army (easy version)
- Spark cluster environment construction
- 【20210923】选择感兴趣的研究方向?
猜你喜欢
Mysql都有那些最需要掌握的原理?
R语言4.04安装教程
[uniapp applet] view container cover-view
VISTA无人驾驶模拟器;FinRL量化金融深度强化学习库;『深度神经网络应用』电子书;CUDA/TensorRT案例集锦;前沿论文 | ShowMeAI资讯日报
ESP8266-Arduino编程实例-ADS1015(ADC)驱动
MySQL database
Kubernetes资源编排系列之四: CRD+Operator篇
【uniapp小程序】视图容器cover-view
【 8.7 】 source code - card to LCM with GCD 】 【 】
JVM-简介&垃圾回收&内存泄漏分析
随机推荐
APICloud AVM 封装日期和时间选择组件
Chapter 20 Source Code File REST API Reference (2)
ggplot2可视化水平箱图并使用fct_reorder排序数据、使用na.rm处理缺失值(reorder boxplot with fct_reorder)、按照箱图的中位数从大到小排序水平箱图
Photoshop2021安装教程
中金财富开户安全吗?怎么操作?
七、jmeter发出请求的逻辑
Redis design and implementation notes (1)
Are Huishang Futures official and reliable?Is it safe to open an account in Huishang Futures?
李沐:机器学习者进阶学习建议
leetcode:296.最佳的碰头地点
谈谈怎么可以得到显著性图 特征图 featuremap
Is it safe to open an account with CICC Wealth?How does it work?
MySQL 数据库
L2-027 名人堂与代金券 (25 分)
Subject: Ordered Queue
L2-024 部落 (25 分)(并查集)
垃圾账号不胜其烦,设备指纹快速发现
【 8.7 】 source code - card to LCM with GCD 】 【 】
Understanding of redis slice cluster
数字图像处理(六)—— 图像压缩