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

思路:不想自己写堆所以用了STL中heap,然后剩下的就比较暴力了,未满十八岁请在监护人的陪同下观看。
#include<string>
#include<vector>
#include<sstream>
#include<iostream>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{ int n, m; cin >> n >> m;
vector<int>vec; for (int i = ; i < n; i++){
int num; cin >> num;
vec.push_back(num);
push_heap(vec.begin(), vec.end(), greater<int>());
}
getchar();
while (m--){
bool flag = false;
string temp;
stringstream ss;
ss.clear();
getline(cin, temp);
ss << temp; if (temp.find("root") < temp.size()){
int cnt; ss >> cnt;
if (vec[] == cnt)flag = true;
}
else if (temp.find("siblings") < temp.size()){
int a, b; string c;
ss >> a >> c >> b; for (int i = ; i < vec.size(); i++){
if (vec[i] == a){
int bro = i % == ? i + : i - ;
if (vec[bro] == b)flag = true;
break;
}
if (vec[i] == b){
int bro = i % == ? i + : i - ;
if (vec[bro] == a)flag = true;
break;
}
}
}
else if (temp.find("parent") < temp.size()){
int er, ba; string a, b, c, d;
ss >> ba >> a >> b >> c >> d >> er; for (int i = ; i < vec.size(); i++)
if (vec[i] == ba){
if ( * i + < vec.size() && vec[ * i + ] == er){
flag = true; break;
}
if ( * i + < vec.size() && vec[ * i + ] == er){
flag = true; break;
}
}
}
else if (temp.find("child") < temp.size()){
int er, ba; string a, b, c, d;
ss >> er >> a >> b >> c >> d >> ba;
for (int i = ; i < vec.size(); i++)
if (vec[i] == ba){
if ( * i + < vec.size() && vec[ * i + ] == er){
flag = true; break;
}
if ( * i + < vec.size() && vec[ * i + ] == er){
flag = true; break;
}
}
}
if (flag)cout << "T" << endl;
else cout << "F" << endl;
} return ;
}
 

L2-012. 关于堆的判断(STL中heap)的更多相关文章

  1. STL中heap相关函数

    heap并不是属于STL中的containers,而是在<algorithm>下提供了相关的函数 make_heap,sort_heap,pop_heap,push_heap 函数的说明: ...

  2. STL中heap算法(堆算法)

     ①push_heap算法 以下是push_heap算法的实现细节.该函数接收两个迭代器,用来表现一个heap底部容器(vector)的头尾,而且新元素已经插入究竟部的最尾端. template ...

  3. STL中heap用法

    #include<cstdio> #include<iostream> #include<algorithm> using namespace std; ]={,, ...

  4. STL中的所有算法(70个)

    STL中的所有算法(70个)----9种类型(略有修改by crazyhacking) 参考自: http://www.cppblog.com/mzty/archive/2007/03/14/1981 ...

  5. STL中的算法

    STL中的所有算法(70个) 参考自:http://www.cppblog.com/mzty/archive/2007/03/14/19819.htmlhttp://hi.baidu.com/ding ...

  6. 『嗨威说』算法设计与分析 - STL中Sort函数的实现原理初探

    本文索引目录: 一.对Sort算法实现的个人阅读体会 二.Sort算法使用的三个排序算法的优点介绍 2.1 插入排序的优缺点 2.2 堆排序的优缺点 2.3 快速排序的优缺点 2.4 新的结合排序—— ...

  7. STL中六大组件

    1)容器(Container),是一种数据结构,如list,vector,和deques ,以模板类的方法提供.为了访问容器中的数据,可以使用由容器类输出的迭代器: 容器(container)用于存放 ...

  8. STL中的set容器的一点总结

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

  9. 3.2 STL中的函数对象类模板

    *: STL中有一些函数对象类模板,如下所示: 1)例如要求两个double类型的x 和y 的积,可以: multiplies<double>()(x,y); 该表达式的值就是x*y的值. ...

随机推荐

  1. openstack cluster 封装

  2. Spark GraphX 聚合操作

    package Spark_GraphX import org.apache.spark.{SparkConf, SparkContext} import org.apache.spark.graph ...

  3. vue学习笔记(1)

    1.检测变化 <ul> <li v-for="item in list">{{item}}</li> </ul> <scrip ...

  4. 面试官:聊一下你对MySQL索引实现原理?

    在数据库中,如果索引太多,应用程序的性能可能会受到影响,如果索引太少,又会对查询性能产生影响.所以,我们要追求两者的一个平衡点,足够多的索引带来查询性能提高,又不因为索引过多导致修改数据等操作时负载过 ...

  5. mysql 的索引hash和b+tree 区别

    索引hash相当于数组,键值对组合,对于id = 6或者status= 2这样条件查询,但是对于id>12等这样,用btree索引最好.

  6. 区间DP UVA 11584 Partitioning by Palindromes

    题目传送门 /* 题意:给一个字符串,划分成尽量少的回文串 区间DP:状态转移方程:dp[i] = min (dp[i], dp[j-1] + 1); dp[i] 表示前i个字符划分的最少回文串, 如 ...

  7. Node“getTextContent() is undefined for the type Node”处理办法

    最近一个项目在MyEclipse导入后总是报getTextContent() is undefined for the type Node错误. 经过查找原来是因为Node类为JDK中自带的(org. ...

  8. Android 性能优化(16)线程优化:Creating a Manager for Multiple Threads 如何创建一个线程池管理类

    Creating a Manager for Multiple Threads 1.You should also read Processes and Threads The previous le ...

  9. js调试技巧汇总中

    由于设备多样性(PC.ios.安卓.pad.tv)以及各设备对js脚本支持性差异性.js兼容性调试显得越来越重要. 0:尽力模仿真实场景下进行调试,迅速定位问题以及提供解决方案. 1:setTimeo ...

  10. Spring.Net学习笔记(0)-错误汇总

    1.错误一:ObjectDefinitionStoreException "Spring.Objects.Factory.ObjectDefinitionStoreException&quo ...