题目描述
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can’t avoid quarrelling, and it only happens between two monkeys who does not know each other. And when it happens, both the two monkeys will invite the strongest friend of them, and duel. Of course, after the duel, the two monkeys and all of there friends knows each other, and the quarrel above will no longer happens between these monkeys even if they have ever conflicted.

Assume that every money has a strongness value, which will be reduced to only half of the original after a duel(that is, 10 will be reduced to 5 and 5 will be reduced to 2).

And we also assume that every monkey knows himself. That is, when he is the strongest one in all of his friends, he himself will go to duel.

一开始有n只孤独的猴子,然后他们要打m次架,每次打架呢,都会拉上自己朋友最牛叉的出来跟别人打,打完之后战斗力就会减半,每次打完架就会成为朋友(正所谓不打不相识o(∩_∩)o )。问每次打完架之后那俩猴子最牛叉的朋友战斗力还有多少,若朋友打架就输出-1.

输入格式
There are several test cases, and each case consists of two parts.

First part: The first line contains an integer N(N<=100,000), which indicates the number of monkeys. And then N lines follows. There is one number on each line, indicating the strongness value of ith monkey(<=32768).

Second part: The first line contains an integer M(M<=100,000), which indicates there are M conflicts happened. And then M lines follows, each line of which contains two integers x and y, indicating that there is a conflict between the Xth monkey and Yth.

有多组数据

输出格式
For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strength value of the strongest monkey among all of its friends after the duel.

输入输出样例
输入 #1复制

5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
输出 #1复制
8
5
5
-1
10


很明显的一道 可并堆+并查集 。当然 无旋treap也可以做,但是代码量大一些。

我们通过并查集维护当前的猴子所在的集合。

用左偏树维护最大值,并且支持堆合并。然后就ok了。

然后每次就是取出堆顶,踢出去,值减半,然后放回来,最后合并两堆。


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1e5+10;
int n,m;
int val[N],f[N],ch[N][2],dis[N];
int find(int x){return x==f[x]?x:f[x]=find(f[x]);}
int merge(int x,int y){if(!x||!y)   return x+y;if(val[x]<val[y])    swap(x,y);ch[x][1]=merge(ch[x][1],y);if(dis[ch[x][1]]>dis[ch[x][0]])    swap(ch[x][1],ch[x][0]);dis[x]=dis[ch[x][1]]+1;return x;
}
signed main(){while(cin>>n){dis[0]=-1;for(int i=1;i<=n;i++)   ch[i][0]=ch[i][1]=dis[i]=0;for(int i=1;i<=n;i++)  scanf("%d",&val[i]),f[i]=i; cin>>m;while(m--){int x,y; cin>>x>>y; int fa=find(x),fb=find(y);if(fa==fb){puts("-1"); continue;}val[fa]>>=1;int rt=merge(ch[fa][0],ch[fa][1]); ch[fa][0]=ch[fa][1]=dis[fa]=0;int art=merge(rt,fa);f[rt]=f[fa]=art;val[fb]>>=1;rt=merge(ch[fb][0],ch[fb][1]);ch[fb][0]=ch[fb][1]=dis[fb]=0;int brt=merge(rt,fb);f[rt]=f[fb]=brt;rt=merge(art,brt);f[art]=f[brt]=rt;printf("%d\n",val[rt]);}}return 0;
}

Monkey King - 左偏树相关推荐

  1. hdu 1512 Monkey King 左偏树

    这题意思是一群一开始互不认识的猴子,可能会打架,打过一场就是朋友,一开始互不相识的猴子打架的时候,不一定自己动手,回去找自己朋友中战斗力最强的猴子,然后,两个打手打架,当然,如果自己最NB时,自己上, ...

  2. HDU 1512 Monkey King 左偏树 + 并查集

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 题意:有n个猴子,一开始每个猴子只认识自己.每个猴子有一个力量值,力量值越大表示这个猴子打架越厉害. ...

  3. P1456 Monkey King 左偏树模板题

    题目链接 https://www.luogu.com.cn/problem/P1456 题意 n只猴子有自己的力量值s,对于每个操作 x,y,在x和y猴子的猴群中分别选出力量最大的猴子,将力量值/2, ...

  4. HDU 1512 Monkey King(左偏堆)

    爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...

  5. Monkey King(左偏树 可并堆)

    我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...

  6. HDU 1512 Monkey King(左偏树+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1512 题       意: 有n个猴子,一开始每个猴子只认识自己.每个猴子有一个力量值,力量值越大表示 ...

  7. 可并堆——左偏树 Leftist Heap

    今天学习了左偏树,这是一个好理解而且好写的数据结构,和二叉堆一样可以在O(1)时间内取出优先级最高的值,O(logn)时间内删除优先级最高的值,不同的是如果要合并两个堆那么二叉堆就只能跪了.而左偏树能 ...

  8. 《程序设计解题策略》——1.6 利用左偏树实现优先队列的合并

    本节书摘来自华章计算机<程序设计解题策略>一书中的第1章,第1.6节,作者:吴永辉 王建德 更多章节内容可以访问云栖社区"华章计算机"公众号查看. 1.6 利用左偏树实 ...

  9. 【洛谷3377】 左偏树(可并堆)

    前言 其实我是不小心翻线性基的时候看见的. Solution 左偏树只会模板,挖坑待补 代码实现 #include<stdio.h> #include<stdlib.h> #i ...

最新文章

  1. [转载]在SQL Server数据库之间进行数据导入导出,OPENDATASOURCE
  2. 网上复制代码需谨慎,莫名其妙报错看这里!
  3. python和java一样吗-Java与Python到底有什么区别和共同点详细对比
  4. IntelliJ IDEA删除所有断点
  5. 安卓微软雅黑字体ttf_618巨献丨精致的悦黑5字重小字体
  6. arrylist和linked list区别
  7. 红帽Openshift:入门–云中的Java EE6
  8. 政府安全资讯精选 2017年第十三期 网信办发布《互联网新闻信息服务新技术新应用安全评估管理规定》;Facebook颁布新广告政策,加强内容安全...
  9. DE16 Continuation: More General Periods
  10. 阿里云部署flask
  11. bzoj 4871: [Shoi2017]摧毁“树状图” [树形DP]
  12. redis安装及使用
  13. 懒人也能变美,AR试妆会让你剁手到停不下来吗?
  14. 1.新建laravel项目
  15. 【DNS区域传输测试/子域暴力破解/横幅版本检测/生成映射】
  16. 惠普WIN10系统突然桌面和状态栏图标狂闪解决办法
  17. Nginx基础应用——日志切割
  18. 890-git安装教程github创建仓库
  19. vue 使用Computed实现数据的动态计算
  20. vagrant+virtualbox搭建centos7

热门文章

  1. STM32F030 IIC2通用读写24C02、24C16、24C32、24C64等例程
  2. 福州华侨中学计算机老师,三尺讲台著妙笔 谱写侨习好韶光——记2015级福州华侨中学实习队工作检查...
  3. 由开发者的人品问题领略测试人员的人品问题
  4. 屏保问题(即背光灯的关闭)
  5. Linux 安装ssh和配置ssh
  6. jq 监听 radio 选择
  7. Mac开发-公证流程记录Notarization-附带脚本
  8. VC中MapX的开发
  9. CSS 选择器 CSS3选择器
  10. concurrent.futures调研