https://pintia.cn/problem-sets/994805046380707840/problems/994805064676261888

将一系列给定数字顺序插入一个初始为空的小顶堆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),分别是插入元素的个数、以及需要判断的命题数。下一行给出区间[内的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>
using namespace std; const int maxn = 2e5 + 10;
int N, M;
int A[maxn], pos[maxn]; void minHeapify(int i){
if(i==1) return; while(i > 1){
if(A[i] < A[i / 2]){
swap(A[i], A[i / 2]);
i /= 2;
} else return;
}
} int get_num(int l, int r, string s) { int i = l, num = 0;
if(s[l] == '-')
i ++;
for(; i <= r; i ++)
num = num * 10 + (s[i] - '0');
if(s[l] == '-')
return 10000 - num;
return 10000 + num;
} int main() {
scanf("%d%d", &N, &M);
for(int i = 1; i <= N; i ++)
scanf("%d", &A[i]); getchar();
for(int i = 1; i <= N; i ++)
minHeapify(i); for(int i = 1; i <= N; i ++)
pos[A[i] + 10000] = i; string s;
for(int m = 0; m < M; m ++) {
getline(cin, s);
string t1 = "", t2 = "";
int len = s.length();
int temp = 0, temp1 = 0, temp2 = 0;
int num1 = 0, num2 = 0, cnt = 0;
if(s.find("root") != -1) { for(int i = 0; i < len; i ++) {
if(s[i] == ' ') {
temp = i - 1;
break;
}
} num1 = get_num(0, temp, s); if(num1 == A[1] + 10000) printf("T\n");
else printf("F\n");
} else if(s[len - 1] == 's') {
for(int i = 0; i < len; i ++) {
if(s[i] == ' ') {
cnt ++;
if(cnt == 1) temp = i - 1;
if(cnt == 2) temp1 = i + 1;
if(cnt == 3) temp2 = i - 1;
}
} num1 = get_num(0, temp, s);
num2 = get_num(temp1, temp2, s); if((pos[num1] / 2) == (pos[num2] / 2)) printf("T\n");
else printf("F\n");
} else if(s.find("child") != -1) {
temp2 = len - 1;
for(int i = 0; i < len; i ++) {
if(s[i] == ' ') {
cnt ++;
if(cnt == 1) temp = i - 1;
else if(cnt == 5) temp1 = i + 1;
}
} num1 = get_num(0, temp, s);
num2 = get_num(temp1, temp2, s); if(pos[num1] / 2 == pos[num2]) printf("T\n");
else printf("F\n");
} else {
temp2 = len - 1;
for(int i = 0; i < len; i ++) {
if(s[i] == ' ') {
cnt ++;
if(cnt == 1) temp = i - 1;
else if(cnt == 5) temp1 = i + 1;
}
} num1 = get_num(0, temp, s);
num2 = get_num(temp1, temp2, s); if(pos[num2] / 2 == pos[num1]) printf("T\n");
else printf("F\n");
}
} return 0;
}

  历尽波折 先是输入数组之后吸掉换行 然后没仔细看数组数字还有负数数组越界每个数字加 10000 然后建最小堆写错了题目要按顺序插入 落泪

PAT L2-012 关于堆的判断的更多相关文章

  1. (PAT)L2-012 关于堆的判断 (最小堆)

    题目链接:https://www.patest.cn/contests/gplt/L2-012 将一系列给定数字顺序插入一个初始为空的小顶堆H[].随后判断一系列相关命题是否为真.命题分下列几种: “ ...

  2. pat 团体天梯赛 L2-012. 关于堆的判断

    L2-012. 关于堆的判断 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的小顶堆H[] ...

  3. codevs 2879 堆的判断

    codevs 2879 堆的判断 http://codevs.cn/problem/2879/ 题目描述 Description 堆是一种常用的数据结构.二叉堆是一个特殊的二叉树,他的父亲节点比两个儿 ...

  4. ->code vs 2879 堆的判断(堆的学习一)

    2879 堆的判断  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold   题目描述 Description 堆是一种常用的数据结构.二叉堆是一个特殊的二叉树,他的父 ...

  5. 堆的判断(codevs 2879)

    2879 堆的判断  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 堆是一种常用的数据结构.二叉堆 ...

  6. L2-012. 关于堆的判断

    L2-012. 关于堆的判断 题目链接:https://www.patest.cn/contests/gplt/L2-012 终于ac了,简直要哭.题目还是很简单的,不过很多坑: 1.寻找x下标时,有 ...

  7. L2-012. 关于堆的判断(STL中heap)

    L2-012. 关于堆的判断   将一系列给定数字顺序插入一个初始为空的小顶堆H[].随后判断一系列相关命题是否为真.命题分下列几种: “x is the root”:x是根结点: “x and y ...

  8. 【小顶堆的插入构造/遍历】PatL2-012. 关于堆的判断

    L2-012. 关于堆的判断 时间限制   将一系列给定数字顺序插入一个初始为空的小顶堆H[].随后判断一系列相关命题是否为真.命题分下列几种: “x is the root”:x是根结点: “x a ...

  9. 【数组模拟-小顶堆的插入构造/遍历】PAT-L2-012.-关于堆的判断--数组模拟

    L2-012. 关于堆的判断 将一系列给定数字顺序插入一个初始为空的小顶堆H[].随后判断一系列相关命题是否为真.命题分下列几种: “x is the root”:x是根结点: “x and y ar ...

随机推荐

  1. UOJ #390. 【UNR #3】百鸽笼

    UOJ #390. [UNR #3]百鸽笼 题目链接 看这道题之前先看一道相似的题目 [PKUWC2018]猎人杀. 考虑类似的容斥: 我们不妨设处理\(1\)的概率. 我们令集合\(T\)中的所有鸽 ...

  2. PHP 使用 ElasticSearch

    环境 php 7.2elasticsearch 6.2 下载elasticsearch-php 6 下载 安装 elasticsearch 下载源文件,解压,重新建一个用户,将目录的所属组修改为此用户 ...

  3. Jmeter之mysql性能测试

    Jmeter官网地址:https://jmeter.apache.org/ 作为开发人员,必要的性能测试还是需要掌握的,虽然配置druid可以比较直观获得sql的执行时间,那些表被访问的比较多等等,但 ...

  4. WaitForSingleObject的详细用法

    在多线程的情况下,有时候我们会希望等待某一线程完成了再继续做其他事情,要实现这个目的,可以使用Windows API函数WaitForSingleObject,或者WaitForMultipleObj ...

  5. java从命令行接受多个数字求和输出

    一·设计思路 1.定义一个整型变量sum,用于接收和 2.利用循环将命令行数字求和 3.输出参数个数以及参数之和 二·流程图 三·程序源代码 public class JavaAppArguments ...

  6. TerraGate SFS 4.5 版本 发布矢量数据使用的Cache数据如何再返回成shapefile文件

    TerraGate SFS 4.5 版本 发布矢量数据使用的Cache数据如何再返回成shapefile文件? 两年前帮一个朋友解决过这个问题: 如果原来用4.5版本的时候,在网络环境下,为了提升调用 ...

  7. IntelliJ IDEA 常用设置 (二)

    一. 代码提示和补充功能有一个特性:区分大小写. 区分大小写的情况是这样的:比如我们在 Java 代码文件中输入 stringBuffer IntelliJ IDEA 是不会帮我们提示或是代码补充的, ...

  8. highcharts为X轴标签添加链接

    $(function () { var categoryLinks = { 'Foo': 'http://www.google.com/search?q=foo', 'Bar': 'http://ww ...

  9. Linux下修改/设置环境变量JAVA_HOME

    export设置只对当前的bash登录session有效.这是存在内存里面的.你可以写入文件一般的文件.之后source它.或者放到/etc/profile 等等的位置里,不同的地方效果不同. 1. ...

  10. @media响应式的屏幕适配

    当页面小于960px的时候执行 @media screen and (max-width: 960px){ body{ background: #000; } } 等于960px尺寸的代码 @medi ...