当前位置:网站首页>OJ:L2-012 关于堆的判断
OJ:L2-012 关于堆的判断
2022-08-09 02:21:00 【WinterShiver】
L2-012 关于堆的判断 (25 分)
将一系列给定数字顺序插入一个初始为空的小顶堆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 <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdio>
#include <bitset>
using namespace std;
const int N = 1003;
class pile{
// 小顶堆的实现,这是一个垃圾模板
public:
int size; // how many numbers
int value[N]; // the numbers
pile(){
size = 0;
}
bool cmp(int i, int j){
return i > j; // 改了这个就是大顶堆
}
void show(){
for(int i=0; i<size; i++){
printf("%d ", value[i]);
}
printf("\n");
}
void insert(int num){
value[size] = num;
insert_adjust(size);
size++;
}
void insert_adjust(int place){
for(int i=place; i>=1; i--){
int father = (i-1)/2;
if(cmp(value[father], value[i]))
swap(value[father], value[i]);
}
}
void sort(){
// 然而是降序
for(int i=size-1; i>=1; i--){
insert_adjust(i); // 使顶部最小
swap(value[0], value[i]); // 创造尾部稳定区
}
}
};
int main(){
int n, m; cin >> n >> m;
pile p1;
for(int i=0; i<n; i++){
int num; cin >> num;
p1.insert(num);
}
/* 24 is the root 26 and 23 are siblings 46 is the parent of 23 23 is a child of 10 */
for(int i=0; i<m; i++){
int opr1, opr2; cin >> opr1;
char tmp1[100]; cin >> tmp1;
bool flag = false;
if(strcmp("and", tmp1) == 0){
// 26 and 23 are siblings
cin >> opr2;
cin >> tmp1; // are
cin >> tmp1; // siblings
for(int i=0; i<(p1.size-1)/2; i++){
if(p1.value[2*i+1] == opr1 && p1.value[2*i+2] == opr2) flag = true;
if(p1.value[2*i+1] == opr2 && p1.value[2*i+2] == opr1) flag = true;
}
}
else{
// is
cin >> tmp1;
if(strcmp("a", tmp1) == 0){
// 23 is a child of 10
cin >> tmp1; // child
cin >> tmp1; // of
cin >> opr2;
for(int i=1; i<p1.size; i++){
if(p1.value[i] == opr1){
flag = p1.value[(i-1)/2] == opr2;
break;
}
}
}
else{
// the
cin >> tmp1;
if(strcmp("root", tmp1) == 0){
// 24 is the root
flag = p1.value[0] == opr1;
}
else{
// 46 is the parent of 23
cin >> tmp1; // of
cin >> opr2;
for(int i=0; i<(p1.size-1)/2; i++){
if(2*i+1 < p1.size && p1.value[i] == opr1 && p1.value[2*i+1] == opr2) flag = true;
if(2*i+2 < p1.size && p1.value[i] == opr1 && p1.value[2*i+2] == opr2) flag = true;
}
}
}
}
if(flag) cout << "T" << endl;
else cout << "F" << endl;
// cout << opr1 << " " << opr2 << endl;
}
return 0;
}
边栏推荐
- 力扣刷题记录3.1-----977. 有序数组的平方
- Likou Brush Question Record 6.1-----203. Remove linked list elements
- Phenomenon 1 during RF debugging
- 数据恢复软件EasyRecovery支持恢复所有类型的文件
- Composer usage record
- js实现数组去重的方式(7种)
- Electromagnetic radiation safety standards and detection methods
- 力扣刷题记录6.1-----203. 移除链表元素
- 软件开发之我的一点想法
- php过滤特殊字符(仅保留中文、字母、数字、下划线)
猜你喜欢
力扣刷题记录1.5-----367. 有效的完全平方数
ApiFile配置环境
配置文件的读取-TOML
2.1-----27. Remove elements
帮助安全红队取得成功的11条建议
力扣刷题记录8.1-----206. 反转链表
史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!
MT4/MQL4 Getting Started to Mastering EA Tutorial Lesson 1 - MQL Language Common Functions (1) OrderSend() Function
Mysql 5.7 into the pit
程序员的日常生活 | 每日趣闻
随机推荐
使网络安全威胁风险更高和成本更高的五个趋势
The most fierce "employee" in history, madly complaining about the billionaire boss Xiao Zha: So rich, he always wears the same clothes!
Phenomenon 1 during RF debugging
力扣刷题记录1.5-----367. 有效的完全平方数
力扣刷题记录7.1-----707. 设计链表
【HNUMSC】C语言第二讲
【izpack】使用izpack为你的程序提供安装程序封装
MT4 / MQL4 entry to the master of EA course lesson two - commonly used functions
How SEMRush finds keywords for advertising
Open3D 计算点云的均值(质心)与协方差
The first lesson of HNUMSC-C language
HCIP-R&S By Wakin自用笔记(3)OSPF之各类LSA及LSA更新规则
数据恢复软件EasyRecovery支持恢复所有类型的文件
史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!
中国SSD产业突围有多难?除了技术“瓶颈”还有哪里挑战?
虹科技术|如何阻止供应链攻击?
【HNUMSC】C language second lecture
Appium常用操作及H5页面元素定位
力扣刷题记录3.1-----977. 有序数组的平方
Likou Brush Question Record 5.1-----59. Spiral Matrix II