将一系列给定数字顺序插入一个初始为空的小顶堆H[]。随后判断一系列相关命题是否为真。命题分下列几种:

  • x is the rootx是根结点;
  • x and y are siblingsxy是兄弟结点;
  • x is the parent of yxy的父结点;
  • x is a child of yxy的一个子结点。

输入格式:

每组测试第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

分析:根据输入依次在堆中插入元素建立小顶堆,因为判断元素间关系需要用位置做判断,因此通过 unordered_map 记录每个元素的数组中的序号,在接收到命题之后直接取出元素对应的位置 pos 就行了。

#include <bits/stdc++.h>
using namespace std;
vector<int> heap;
int len = 0;
void insert(int x) {
int hole = ++len;
for (; hole > 1 && heap[hole / 2] > x; hole /= 2)
heap[hole] = move(heap[hole / 2]);
heap[hole] = move(x);
}
int main() {
int n, m, a, b;
scanf("%d%d", &n, &m);
heap.resize(n + 1);
for (int i = 0; i < n; i++) {
scanf("%d", &a);
insert(a);
}
unordered_map<int, int> pos;
for (int i = 1; i <= n; i++) pos[heap[i]] = i;
char temp[10];
for (int i = 0; i < m; i++) {
scanf("%d %s", &a, temp);
if (strcmp(temp, "and") == 0) {
scanf("%d %*s %*s", &b);
printf("%c\n", pos[a] / 2 == pos[b] / 2 ? 'T' : 'F');
} else {
scanf("%*s %s", temp);
if (strcmp(temp, "root") == 0) printf("%c\n", a == heap[1] ? 'T' : 'F');
else {
scanf("%*s %d", &b);
if (strcmp(temp, "parent") == 0) printf("%c\n", pos[a] == pos[b] / 2 ? 'T' : 'F');
else
printf("%c\n", pos[b] == pos[a] / 2 ? 'T' : 'F');
}
}
}
return 0;
}

L2-012 关于堆的判断 (25分) (字符串處理)的更多相关文章

  1. PAT 1006 Sign In and Sign Out (25分) 字符串比较

    题目 At the beginning of every day, the first person who signs in the computer room will unlock the do ...

  2. PTA 05-树7 堆中的路径 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/713 5-5 堆中的路径   (25分) 将一系列给定数字插入一个初始为空的小顶堆H[] ...

  3. L2-032 彩虹瓶 (25 分)

    L2-032 彩虹瓶 (25 分)   彩虹瓶的制作过程(并不)是这样的:先把一大批空瓶铺放在装填场地上,然后按照一定的顺序将每种颜色的小球均匀撒到这批瓶子里. 假设彩虹瓶里要按顺序装 N 种颜色的小 ...

  4. PAT 甲级 1060 Are They Equal (25 分)(科学计数法,接连做了2天,考虑要全面,坑点多,真麻烦)

    1060 Are They Equal (25 分)   If a machine can save only 3 significant digits, the float numbers 1230 ...

  5. PTA 字符串关键字的散列映射(25 分)

    7-17 字符串关键字的散列映射(25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位:再用除留余 ...

  6. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  7. L2-013 红色警报 (25 分)

    L2-013 红色警报 (25 分)   战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全 ...

  8. PAT A1122 Hamiltonian Cycle (25 分)——图遍历

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  9. PAT A1142 Maximal Clique (25 分)——图

    A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the ...

  10. 1122 Hamiltonian Cycle (25 分)

    1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...

随机推荐

  1. Linux删除‘-’开头的文件

    版权声明:原创作品,谢绝转载!否则将追究法律责任. ----- 作者:kirin 先看两个特殊文件(以--开头) [root@kirin ~]# ll total 0 -rw-r--r-- 1 roo ...

  2. 马云说的AI电商时代是什么

    这两天非常火的就是马老师说的,我们已经进入了AI的电商时代.相信电商时代大家很容易理解,换一个简单的方式来说就是网上购物. AI相信大家已经很熟悉了,就是人工智能.早在十年前其实已经有AI人工智能的概 ...

  3. The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission,iphone手机video标签报错

    The request is not allowed by the user agent or the platform in the current context, possibly becaus ...

  4. 开发必备工具、插件【ME】

    工欲善其事,必先利其器,记录自己开发中常用的工具.插件:  慢慢更新... 一.必备 1.Visual Studio.IntelliJ IDEA .Sublime Text 3 2.TeamViewe ...

  5. [CF1229E]Marek and Matching

    This is a harder version of the problem. In this version, \(n \le 7\). Marek is working hard on crea ...

  6. [ABC265B] Explore

    Problem Statement Takahashi is exploring a cave in a video game. The cave consists of $N$ rooms arra ...

  7. [ABC262F] Erase and Rotate

    Problem Statement You are given a sequence $P = (p_1,p_2,\ldots,p_N)$ that contains $1,2,\ldots,N$ e ...

  8. 微调baichuan2-7b遇到的显存坑

    问题描述: 微调baichuan2-7b模型,验证一轮后继续训练第一个iteration显存大幅增加 项目链接: https://github.com/wp931120/baichuan_sft_lo ...

  9. 高斯朴素贝叶斯(Gaussian Naive Bayes)原理与实现——垃圾邮件识别实战

    朴素贝叶斯(Naive Bayes): 根据贝叶斯定理和朴素假设提出的朴素贝叶斯模型. 贝叶斯定理: 朴素假设(特征条件独立性假设): 代入可知朴素贝叶斯模型计算公式: 因为朴素贝叶斯是用来分类任务, ...

  10. 组合式api-计算属性computed的使用

    计算属性在vue3中和vue2的思想概念都是一样,唯一区别就是在使用组合式api时候的语法稍有不同. 使用步骤: 导入computed函数 import {computed} from 'vue' 使 ...