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 ...
随机推荐
- ajax 小练习
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="Con ...
- django_admin用法
Django内置的admin Django内置的Admin是对于model中对应的数据表进行增删改查提供的组件,使用方式有: 依赖APP: django.contrib.auth django.con ...
- maven_环境变量配置
- GitHub for Windows客户端使用操作流程
Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理.作为一个程序员,我们需要掌握其用法. 作为开源代码库以及版本控制系统,Github目前拥有140 ...
- Spring的RestTemplate
Spring提供了一个RestTemplate模板工具类,对基于Http的客户端进行了封装,并且实现了对象与json的序列化和反序列化,非常方便.RestTemplate并没有限定Http的客户端类型 ...
- jackson工具类 对象转字符串 -- 字符串转对象
这个一个json的工具类.用的是jackson,当然还有谷歌的gosn,阿里的fastjson ,但是jackson的感觉还是最成熟(网上大神说的...) 实现的功能很简单,对象转字符串 字符串转简 ...
- Hadoop记录-Hadoop集群重要监控指标
通用监控指标 对于每个RPC服务应该监控 RpcProcessingTimeAvgTime(PRC处理的平均时间) 通常hdfs在异常任务突发大量访问时,这个参数会突然变得很大,导致其他用户访问hdf ...
- Spark设计理念与基本架构
1.基本概念 Spark中的一些概念: RDD(resillient distributed dataset):弹性分布式数据集. Partition:数据分区.即一个RDD的数据可以划分为多少个分区 ...
- 细说log4j之概述
log4j官网:https://logging.apache.org/ log4j目前存在2个版本:log4j 1.x 和log4j 2.x,目前官方主推2.x版本(log4j 1.x已于2015.0 ...
- MySQL 数据库应用程序编程
普通用户使用客户端应用程序(Client Application)和服务器程序(Server Application)通信以取得服务, 而服务器程序通常要和数据库服务器通信以取得数据存取服务, 这时就 ...