pat 团体天梯赛 L2-012. 关于堆的判断
L2-012. 关于堆的判断
将一系列给定数字顺序插入一个初始为空的小顶堆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 思路:堆排序,字符串处理可以用stringstream
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<set>
#include<queue>
#include<sstream>
using namespace std;
#define N_MAX 1000+2
#define INF 0x3f3f3f3f
int n, m;
vector<int>vec;
bool cmp(const int &a,const int &b) {
return a > b;
} int main() {
while (cin>>n>>m) {
vec.clear();
for (int i = ; i < n; i++) { int a; cin >> a; vec.push_back(a); make_heap(vec.begin(), vec.end(), cmp); }
getchar();//吸收空格
while (m--) {
string s; getline(cin, s);
stringstream ss(s);
if (s[s.size() - ] == 't') {
int a;
ss >> a;
if (a == vec[])puts("T");
else puts("F");
}
else if (s[s.size()-]=='s') {
int a, b; string tmp;
ss >> a >> tmp >> b;
int pos_a = find(vec.begin(), vec.end(), a) - vec.begin()+;
int pos_b = find(vec.begin(), vec.end(), b) - vec.begin()+;
if (pos_a / == pos_b / )puts("T");
else puts("F");
}
else {
int a, b; string tmp;
ss >> a >> tmp >> tmp;
int pos_a = find(vec.begin(), vec.end(), a) - vec.begin() + ;
if (tmp[] == 't') {
ss >> tmp >> tmp >> b;
int pos_b = find(vec.begin(), vec.end(), b) - vec.begin() + ;
if (pos_b / == pos_a)puts("T");
else puts("F");
}
else {
ss >> tmp >> tmp >> b;
int pos_b = find(vec.begin(), vec.end(), b) - vec.begin() + ;
if (pos_a / == pos_b)puts("T");
else puts("F");
} }
}
}
return ;
}
pat 团体天梯赛 L2-012. 关于堆的判断的更多相关文章
- pat 团体天梯赛 L3-007. 天梯地图
L3-007. 天梯地图 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校 ...
- pat 团体天梯赛 L3-015. 球队“食物链”
L3-015. 球队“食物链” 时间限制 1000 ms 内存限制 262144 kB 代码长度限制 8000 B 判题程序 Standard 作者 李文新(北京大学) 某国的足球联赛中有N支参赛球队 ...
- pat 团体天梯赛 L1-039. 古风排版
L1-039. 古风排版 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 中国的古人写文字,是从右向左竖向排版的.本题就请你编写 ...
- pat 团体天梯赛 L3-010. 是否完全二叉搜索树
L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...
- pat 团体天梯赛 L3-009. 长城
L3-009. 长城 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 邓俊辉(清华大学) 正如我们所知,中国古代长城的建造是为了抵御外 ...
- pat 团体天梯赛 L2-011. 玩转二叉树
L2-011. 玩转二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜 ...
- pat 团体天梯赛 L2-010. 排座位
L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...
- pat 团体天梯赛 L2-007. 家庭房产
L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产 ...
- pat 团体天梯赛 L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
随机推荐
- Java基础面试操作题: 获取 1-20 之间的随机数,共计 20 个,要求不能重复 获取 1-20 之间的随机数,共计 10 个,要求不能重
package com.swift; import java.util.HashSet; import java.util.Random; import java.util.Set; public c ...
- NSCopying
///< .h @interface ChatManager : NSObject <NSCopying> @property (nonatomic) NSUInteger inde ...
- js点击拉拽轮播图pc端移动端适配
<div class="content"> <button class="left">left</button> <b ...
- d3.js--01
D3 的全称是(Data-Driven Documents),顾名思义可以知道是一个被数据驱动的文档.听名字有点抽象,说简单一点,其实就是一个 JavaScript 的函数库,使用它主要是用来做数据可 ...
- JDK和CGLIB动态代理原理区别
JDK和CGLIB动态代理原理区别 https://blog.csdn.net/yhl_jxy/article/details/80635012 2018年06月09日 18:34:17 阅读数:65 ...
- 爬虫学习(四)——post请求爬取
百度翻译爬取数据 import urllib.requestimport urllib.parsepost_url = "https://fanyi.baidu.com/sug"h ...
- 问题:Could not install packages due to an EnvironmentError: [Errno 13] Permission denied:
1.安装django 执行pip3 install --user django 2.解决方法:加--user 执行pip3 install --user django
- python 3 在工作中的应用
Python 3在工作中的使用 安装配置Python 3 在notepad++中配置Python 3 使用sql server数据库 操作Excel 发送email python 3 使用日志 安 ...
- The Moving Points - HDU - 4717 (模拟退火)
题意 二维空间中有\(n\)个运动的点,每个点有一个初始坐标和速度向量.求出一个时间\(T\),使得此时任意两点之间的最大距离最小.输出\(T\)和最大距离. 题解 模拟退火. 这个题告诉了我,初始步 ...
- DFS:POJ1190-生日蛋糕(基础搜索)
生日蛋糕 Time Limit: 1000MS Memory Limit: 10000K 描述 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. ...