当前位置:网站首页>Judgment on heap (25 points) two insertion methods
Judgment on heap (25 points) two insertion methods
2022-04-23 08:41:00 【Huaihua second affectionate】
Insert a sequence of given numbers into a small, initially empty top heap H[]
. Then judge whether a series of related propositions are true . There are several kinds of propositions :
x is the root
:x
It is the root node. ;x and y are siblings
:x
andy
It's a brotherhood ;x is the parent of y
:x
yesy
The parent node ;x is a child of y
:x
yesy
A child node of .
Input format :
Each group of tests 1 Line inclusion 2 A positive integer N
(≤ 1000) and M
(≤ 20), They are the number of inserted elements 、 And the number of propositions to be judged . The next line gives the interval [−10000,10000] Internal N
An integer to be inserted into a small top heap that is initially empty . after M
That's ok , Each line gives a proposition . The topic guarantees that the node key values in the proposition all exist .
Output format :
For each proposition of input , If it's true , Output in one line T
, Otherwise output F
.
sample input :
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
sample output :
F
T
F
T
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int N = 10010;
int n, m;
int h[N], cnt=0;
// void up(int u) {
// while (h[u] < h[u >> 1]&&u/2>0 ) {
// swap(h[u], h[u >> 1]);
// u >>= 1;
// }
// }
void insert(int u){
h[++cnt]=u;
int i=cnt;
while(i/2&&h[i]<h[i/2]){
swap(h[i],h[i/2]);
i/=2;
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
// h[++cnt] = x;
// up(i);
insert(x);
}
unordered_map<int, int> mp;
for (int i = 1; i <= n; i++) mp[h[i]] = i;
while (m--) {
int a;
cin >> a;
string s1;
cin >> s1;
if (s1 == "and") {
int b;
cin >> b;
cin >> s1 >> s1;
if (mp[a] / 2 == mp[b] / 2) puts("T");
else puts("F");
}
else {
cin >> s1;
if (s1 == "a") {
cin >> s1 >> s1;
int b;
cin >> b;
if (mp[a] / 2 == mp[b]) puts("T");
else puts("F");
}
else {
cin >> s1;
if (s1 == "root") {
if (a == h[1]) puts("T");
else puts("F");
}
else {
cin >> s1;
int b;
cin >> b;
if (mp[b] / 2 == mp[a]) puts("T");
else puts("F");
}
}
}
}
return 0;
}
版权声明
本文为[Huaihua second affectionate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230835222469.html
边栏推荐
猜你喜欢
随机推荐
请问中衍期货安全靠谱吗?
PgSQL wants to implement all kinds of column sub query operations of MySQL
Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动
IDEA导入commons-logging-1.2.jar包
Kubernetes如何使用harbor拉去私有镜像
使用flask和h5搭建网站/应用的简要步骤
洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
idea打包 jar文件
Redis master-slave server problem
Add random attributes to the Li class array objects and sort them
Go语言自学系列 | golang结构体的初始化
Shell script advanced
Use of Arthas in JVM tools
还原二叉树 (25 分)
引用传递1
MySQL查询两张表属性值非重复的数据
1099 establish binary search tree (30 points)
excle加水印
Get the absolute path of the class according to the bytecode
玩转二叉树 (25 分)