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 ...
随机推荐
- httprouter使用pprof
httprouter使用pprof 参考:https://github.com/feixiao/httpprof 性能分析参考:https://github.com/caibirdme/hand-to ...
- python机器学习-sklearn挖掘乳腺癌细胞(四)
python机器学习-sklearn挖掘乳腺癌细胞( 博主亲自录制) 网易云观看地址 https://study.163.com/course/introduction.htm?courseId=10 ...
- netty的数据通信之心跳检测
问题1:我们想实现客户端和服务端建立连接之后,5秒钟之后如果没有数据传输就关闭与客户端的连接. 解决办法:在服务端加上下面一条代码 ch.pipeline().addLast(new ReadTime ...
- python 面向对象(六)MRO C3算法 super
########################总结################ 面向对象回顾 类:对某一个事物的描述,对一些属性和方法的归类 class 类名: var=123#类变量 def ...
- Linux记录-HDFS副本机制
1. 副本策略 NameNode具有RackAware机架感知功能,这个可以配置. 若client为DataNode节点,那存储block时,规则为:副本1,同client的节点上:副本2,不同机架节 ...
- PHP中的表单传值
前言:试想一下如果PHP没有了表单传值,那么就相当于一个没有表达能力的人,因此表单传值在php中是相当重要的,但是不难,接下来我们一起来探讨一下吧------> (一)什么是表单传值 概念:表 ...
- 用itext合并多个pdf文件【转】【补】
java代码 package c; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arra ...
- redis集群之哨兵模式【原】
redis集群之哨兵(sentinel)模式 哨兵模式理想状态 需要>=3个redis服务,>=3个redis哨兵,每个redis服务搭配一个哨兵. 本例以3个redis服务为例: 一开始 ...
- golang cronexpr定时任务包使用
包获取 go get -u github.com/gorhill/cronexpr 创建一个定时任务 expr, err = cron.Parse("* * * * *"); 获得 ...
- Lost connection to MySQL server during query,MySQL设置session,global变量及网络IO与索引
Navicat导出百万级数据时,报错:2013 - Lost connection to MySQL server during query 网上一番搜索,修改mysql如下几处配置文件即可: sel ...