当前位置:网站首页>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:xIt is the root node. ;x and y are siblings:xandyIt's a brotherhood ;x is the parent of y:xyesyThe parent node ;x is a child of y:xyesyA 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
边栏推荐
猜你喜欢
随机推荐
汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
《深度学习》学习笔记(八)
okcc呼叫中心外呼系统智能系统需要用多大的盘存录音?
DOM 学习之—添加+-按钮
在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
Shell脚本进阶
Kubernetes如何使用harbor拉去私有镜像
增强现实技术是什么?能用在哪些地方?
jsp页面编码
Transformer XL: attention language modelsbbeyond a fixed length context paper summary
'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
是否同一棵二叉搜索树 (25 分)
Introduction to protobuf
How much inventory recording does the intelligent system of external call system of okcc call center need?
RPC过程
IDEA导入commons-logging-1.2.jar包
求简单类型的矩阵和
对li类数组对象随机添加特性,并进行排序
匿名类型(C# 指南 基础知识)
STM32 uses Hal library. The overall structure and function principle are introduced









