PAT 天梯赛 L2-012. 关于堆的判断 【Heap】【STL】
题目链接
https://www.patest.cn/contests/gplt/L2-012
思路
使用 STL 里面有关 Heap 的函数
std::make_heap将[start, end)范围进行堆排序,默认使用less, 即最大元素放在第一个。
std::pop_heap将front(即第一个最大元素)移动到end的前部,同时将剩下的元素重新构造成(堆排序)一个新的heap。
std::push_heap对刚插入的(尾部)元素做堆排序。
std::sort_heap将一个堆做排序,最终成为一个有序的系列,可以看到sort_heap时,必须先是一个堆(两个特性:1、最大元素在第一个 2、添加或者删除元素以对数时间),因此必须先做一次make_heap.
make_heap, pop_heap, push_heap, sort_heap都是标准算法库里的模板函数,用于将存储在vector/deque 中的元素进行堆操作
根据 题目的意思 是要用 push_heap 一个一个 插入
而不能用 make_heap
因为 push_heap 和 make_heap 构造的堆 是不一样的
比如
3 6 2 4 1 5 这组数据
make_heap
push_heap
然后 用一个 map 将元素 与其 数组下标 对应起来 要从1 开始
根节点 就是判断一下 其数组下标 是不是 1
x 和 y 是兄弟节点
就是 判断一下
a = x 的数组下标 / 2
b = y 的数组下标 / 2
a 对应的数 和b 对应的数 是不是相等 并且 a != b
x 是 y 的父节点
就是 判断一下 y 的数组下标 / 2 是不是 等于 x 的数组下标
x 是
x 是 y 的子节点
就是 判断一下 x 的数组下标 / 2 是不是 等于 y 的数组下标
AC 代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a) memset(a, 0, sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int MOD = 1e9 + 7;
int main()
{
map <int, int> vis;
int n, m, num;
scanf("%d%d", &n, &m);
vector <int> v;
for (int i = 0; i < n; i++)
{
scanf("%d", &num);
num = -num;
v.push_back(num);
push_heap(v.begin(), v.end());
}
for (int i = 0; i < n; i++)
vis[-v[i]] = i + 1;
while (m--)
{
int a, b, c, d;
string s;
cin >> a;
cin >> s;
if (s == "and") //2
{
cin >> b;
cin >> s;
cin >> s;
c = vis[a];
d = vis[b];
if (v[c / 2] == v[d / 2] && c != d)
cout << "T\n";
else
cout << "F\n";
}
else
{
cin >> s;
if (s == "the")
{
cin >> s;
if (s == "root") //1
{
if (vis[a] == 1)
cout << "T\n";
else
cout << "F\n";
}
else // 3
{
cin >> s;
cin >> b;
c = vis[a];
d = vis[b];
if (d / 2 == c)
cout << "T\n";
else
cout << "F\n";
}
}
else //4
{
cin >> s;
cin >> s;
cin >> b;
c = vis[a];
d = vis[b];
if (c / 2 == d)
cout << "T\n";
else
cout << "F\n";
}
}
}
}
最新文章
- 目标电脑未安装VC++6.0或者VS,运行APP丢失DLL问题解决办法
- jquery实现导航图轮播
- c++ 虚函数和纯虚函数
- scipy科学计算库
- SVN使用教程
- HDU 5015 233 Matrix --矩阵快速幂
- php利用svn hooks将程序自动发布到测试环境
- IOS第八天(1:UITableViewController团购,数据转模型,xib显示数据)
- OA 办公自动化系统:权限管理模块的实现原理思路
- I Count Two Three---hdu5878(打表+二分)
- HTTP断点续传的基本原理
- 张王李相亲应用if else
- android 实现真正意义上的服务及源代码下载
- Ubuntu Linux下设置IP的配置命令
- Net core 关于缓存的实现
- LaLeX数学公式
- 18-11-05ie 热键的使用
- Linux系统学习之软件安装
- 解决:Host xxx.xxx.xxx.xxx is blocked because of many connection errors.
- 《linux内核分析》作业一:分析汇编代码
热门文章
- Directional Light,Ambient,Specular,光照感性认识...
- iOS代码覆盖率测试工具
- C#实现发送手机短信
- declare @t table
- Snail—UI学习之UITextField
- linux进程状态详解(转)
- Ubuntu 16.04主题美化和软件推荐
- 在spring mvc中利用ajax批量删除数据
- Android linux kernel privilege escalation vulnerability and exploit (CVE-2014-4322)
- maven 依赖文件 pom.xml 编译 mvn compile 运行 不用mvn exec:java -Dexec.mainClass=";hello.HelloWorld"; 打成jar包 mvn package mvn install http://blog.csdn.net/yaya1943/article/details/48464371