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 ...
随机推荐
- win命令大全
Windows系统:开始--运行--命令大全 Nslookup-------IP地址侦测器 explorer-------打开资源管理器 logoff--- ...
- SQLMAP UDF提权
SQLMAP UDF提权 1.连接mysql数据打开一个交互shell: sqlmap.py -d mysql://root:root@127.0.0.1:3306/test --sql-s ...
- bzoj1061 建图 + 最小费用流
https://www.lydsy.com/JudgeOnline/problem.php?id=106152 对于一个点对上多个点,不太容易建图的时候,考虑逆向思考 申奥成功后,布布经过不懈努力,终 ...
- JavaSet接口、唯一元素和Map接口整理
Set接口 1.1 HashSet集合存储数据的结构(哈希表) HashSet集合,采用哈希表结构存储数据,保证元素唯一性的方式依赖于:hashCode()与equals()方法. 哈希表底层,使用的 ...
- 关于java文件下载文件名乱码问题解决方案
JAVA文件下载时乱码有两种情况: 1,下载时中文文件名乱码 2,下载时因为路径中包含中文文件名乱码,提示找不到文件 解决方法见下面部分代码 response.setContentType(" ...
- go实现Windows服务注册
go实现Windows服务注册 1.nssm下载:http://nssm.cc/download 2.服务注册 nssm.exe install 服务名 程序 样例如下: .\nssm.exe in ...
- python机器学习-sklearn挖掘乳腺癌细胞(一)
python机器学习-sklearn挖掘乳腺癌细胞( 博主亲自录制) 网易云观看地址 https://study.163.com/course/introduction.htm?courseId=10 ...
- MySQL语句_积累
1.GROUP_CONCAT 直接返回拼接好的多个结果,可以供IN()函数使用 语句 结果 +-----------------------------------------+ | GROUP_CO ...
- springboot整合mybatis出现的一些问题
springboot整合mybatis非常非常的简单,简直简单到发指.但是也有一些坑,这里我会详细的指出会遇到什么问题,并且这些配置的作用 整合mybatis,无疑需要mapper文件,实体类,dao ...
- redis做session会话共享
项目中需要两个不同的web项目互相访问,用户对象为同一个User.决定用Redis来存储用户对象信息...ok,环境搭建开始: 1.pom.xml引入Redis依赖的jar: <!-- jedi ...