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 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
随机推荐
- GVIM——简直美如画,有没有!
"========================================== " Author: wklken " Version: 9.1 " Em ...
- Java读取各种文件格式内容
所需的jar包哦也不要太记得了,大家可以搜搜,直接上代码: import java.io.BufferedInputStream; import java.io.File; import java.i ...
- 判断IP连接数前五,并自动加入防火墙
#!/bin/bash #Author Template #Time -- : log_file=/tmp/tmp.log JudgeExt(){ if expr "$1" : & ...
- .net core 获取浏览器UserAgent
这两天由于自己公司的机器磁盘不够用了,果断把VS2015卸载了,只留下VS2017 当我打开一个以前一个很简单的MVC4.0的项目时候 温馨提示要安装MVC4,我犹豫了一下,还是点了安装,接下来提示要 ...
- CentOS下安装php gd库报错Error: php56w-common conflicts with php-common-5.3.3-48.el6_8.x86_64
因为服务器缺少php gd库,因为系统是centos,就是用yum去安装,一安装就报错如下: [root@iZ28sdxghs2Z ~]# yum install php-gd Loaded plug ...
- 数据结构-哈夫曼(Huffman)
#include <iostream> #include <cstdio> #include <malloc.h> #define LIST_INIT_SIZE 1 ...
- day 71 Django基础六之ORM中的锁和事务
Django基础六之ORM中的锁和事务 本节目录 一 锁 二 事务 三 xxx 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 锁 行级锁 select_for_update(no ...
- 将Excel文件转为csv文件的python脚本
#!/usr/bin/env python __author__ = "lrtao2010" ''' Excel文件转csv文件脚本 需要将该脚本直接放到要转换的Excel文件同级 ...
- 跨域问题和django中实现跨域
跨域问题 1.同源策略(浏览器的安全功能): 请求的url地址,必须与浏览器上的url地址处于同域上,也就是域名,端口,协议相同 2.CORS跨域资源共享 实现CORS通信的关键是服务器,只要服务器实 ...
- 笔记-python-standard library-8.10 copy
笔记-python-standard library-8.10 copy 1. copy source code:Lib/copy.py python中的赋值语句不复制对象,它创建了对象和目 ...