P1456 Monkey King
题目地址:P1456 Monkey King
一道挺模板的左偏树题
然后你发现看完论文打完模板之后就可以A掉这道题不用回来了
细节见代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 6;
int n, m, f[N], a[N], l[N], r[N], d[N];
//类并查集路径压缩
int get(int x) {
if (x == f[x]) return x;
return f[x] = get(f[x]);
}
//左偏树合并
inline int merge(int x, int y) {
if (!x || !y) return x + y;//x或y为0则返回另一个的简写
if (a[x] < a[y]) swap(x, y);//保证堆性质
r[x] = merge(r[x], y);
f[r[x]] = x;
if (d[l[x]] < d[r[x]]) swap(l[x], r[x]);//保证左偏树性质
d[x] = d[r[x]] + 1;
return x;
}
//删除堆顶,注意语句顺序
inline void pop(int x) {
f[l[x]] = l[x], f[r[x]] = r[x];
f[x] = merge(l[x], r[x]);
l[x] = r[x] = 0;
}
//多组数据单独写一个函数
inline void work() {
d[0] = -1;//0的“距离”为-1
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
f[i] = i;
d[i] = l[i] = r[i] = 0;//多组数据清空数组
}
cin >> m;
while (m--) {
int x, y;
scanf("%d %d", &x, &y);
int fx = get(x), fy = get(y);//找堆顶
if (fx == fy) puts("-1");//若在同一个堆中输出-1
else {
pop(fx), pop(fy);//删除堆顶
a[fx] >>= 1, a[fy] >>= 1;
f[fx] = merge(fx, f[fx]), f[fy] = merge(fy, f[fy]);//将新值插入堆顶
fx = get(fx), fy = get(fy);
printf("%d\n", a[merge(fx,fy)]);//输出堆顶,注意堆中存的是下标而不是数
}
}
}
int main() {
while (cin >> n) work();
return 0;
}
P1456 Monkey King的更多相关文章
- 洛谷P1456 Monkey King
https://www.luogu.org/problemnew/show/1456 #include<cstdio> #include<iostream> #include& ...
- 【luogu P1456 Monkey King】 题解
题目链接:https://www.luogu.org/problemnew/show/P1456 左偏树并查集不加路径压缩吧... #include <cstdio> #include & ...
- ZOJ 2334 Monkey King
并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子 Monkey King ...
- 数据结构(左偏树):HDU 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- HDU - 5201 :The Monkey King (组合数 & 容斥)
As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...
- Monkey King(左偏树 可并堆)
我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...
- 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- The Monkey King(hdu5201)
The Monkey King Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- hdu1512 Monkey King
Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each ...
随机推荐
- Storm 消息分发策略
1.Shuffle Grouping:随机分组,随机派发stream里面的tuple,保证每个bolt接收到的tuple数目相同.2.Fields Grouping:按字段分组,比如按userid来分 ...
- saltstack SLS 安装haproxy+nginx实例分析学习
本文主要以实例的形式去熟悉sls的部署流程及相关模块的使用 文件下载:https://github.com/unixhot/saltbook-code 目录结构 [root@k8s_master sa ...
- 运维监控-Zabbix Server 使用QQ SMTP发送邮件报警及定制报警内容
运维监控-Zabbix Server 使用QQ SMTP发送邮件报警及定制报警内容 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客采用腾讯邮箱,想必大家都对QQ很了解,所以 ...
- spark常见异常汇总
spark常见异常汇总 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 温馨提示: 如果开发运行spark出现问题啦,可能需要运维这边做一些调优,也可能是开发那边需要修改代码.到 ...
- cookies、sessionStorage和localStorage解释及区别
在浏览器查看 HTML4的本地存储 cookie 浏览器的缓存机制提供了可以将用户数据存储在客户端上的方式,可以利用cookie,session等跟服务端进行数据交互. 一.cookie和sessio ...
- Quartus II 中 Verilog 常见警告/错误汇总
Verilog 常见错误汇总 1.Found clock-sensitive change during active clock edge at time <time> on regis ...
- 使用JMeter进行一次简单的带json数据的post请求测试,json可配置参数
配置: 1.新建一个线程组: 然后设置线程数.运行时间.重复次数. 2.新建Http请求: 设置服务器域名,路径,方法,编码格式,数据内容. 可以在函数助手中,编辑所需要的变量,比如本例中的随机生成电 ...
- Java Service Wrapper 使用
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sinat_26279177/article/details/70807173 1 简介 ...
- springBoot----aop--整合日志相关
springBoot整合日志相关 1:新建log4j.properties文件 : log4j.properties: #log4j.rootLogger=CONSOLE,info,error,DEB ...
- springBoot整合mybatis、jsp 或 HTML
springBoot整合mybatis.jsp Spring Boot的主要优点: 1: 为所有Spring开发者更快的入门: 2: 开箱即用,提供各种默认配置来简化项目配置: 3: 内嵌式容器 ...