HDU - 1512 Monkey King
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.
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.
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
5
5
-1
10
可并堆裸题,没什么思维难度。只要学会了就会打了.
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<stdlib.h>
const int MANX=;
using namespace std;
int fa[MANX],r[MANX],l[MANX],v[MANX],d[MANX];
int n,m; void cl(){
for(int i=;i<=n;i++) fa[i]=i;
memset(r,,sizeof(r));
memset(l,,sizeof(l));
memset(v,,sizeof(v));
} int find(int x){
if(fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
} int merge(int x,int y){
if(!x) return y;
if(!y) return x;
if(v[x]<v[y]) swap(x,y);
r[x]=merge(r[x],y);fa[r[x]]=x;
if(d[r[x]]>d[l[x]]) swap(l[x],r[x]);
else d[x]=d[r[x]]+;//
return x;
} int del(int x){
int lz=l[x],rz=r[x];
l[x]=r[x]=d[x]=;fa[lz]=lz,fa[rz]=rz;
return merge(lz,rz);
} int main(){
while(scanf("%d",&n)!=EOF){
cl();
for(int i=;i<=n;i++) scanf("%d",&v[i]);
scanf("%d",&m);
for(int i=;i<=m;i++){
int x,y;scanf("%d%d",&x,&y);
int ll=find(x),rr=find(y);
if(ll==rr){printf("-1\n");}
else{
v[ll]/=,v[rr]/=;
int lz=del(ll),rz=del(rr);
lz=merge(ll,lz),rz=merge(rz,rr);
printf("%d\n",v[merge(lz,rz)]);
}
}
}
}
HDU - 1512 Monkey King的更多相关文章
- 【HDOJ】【1512】Monkey King
数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...
- hdu 5201 The Monkey King【容斥原理+组合数学】
原来我一开始以为的\( O(n^2) \)是调和级数\( O(nlog_2n) \)的! 首先枚举猴王的桃子个数\( x \),然后使用容斥原理,枚举有至少\( k \)个不满足的条件,那么这\( k ...
- 数据结构(左偏树):HDU 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- hdu 1512 Monkey King 左偏树
题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...
- HDU - 5201 :The Monkey King (组合数 & 容斥)
As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...
- 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Monkey King(左偏树 可并堆)
我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...
- ZOJ 2334 Monkey King
并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子 Monkey King ...
- P1456 Monkey King
题目地址:P1456 Monkey King 一道挺模板的左偏树题 不会左偏树?看论文打模板,完了之后再回来吧 然后你发现看完论文打完模板之后就可以A掉这道题不用回来了 细节见代码 #include ...
随机推荐
- SpringBoot 参数校验的方法
Introduction 有参数传递的地方都少不了参数校验.在web开发中,前端的参数校验是为了用户体验,后端的参数校验是为了安全.试想一下,如果在controller层中没有经过任何校验的参数通过s ...
- Markdown 直接转换公众号文章,不再为排版花时间
上一篇「又一家数据公司被查,爬虫到底做错了什么?」反响强烈,虽然我这是新号,但还是获得了不少公众号的转发,借机也结识了很多业内大佬,在此感谢大家的抬爱! 同时也有不少号主问我的文章排版是用的哪个网站, ...
- 行内元素有哪些?块级元素有哪些?空(void)元素有哪些?
CSS规范规定,每个元素都有display属性,确定该元素的类型.每个元素都有默认的display值,如div的display默认值为“block”,则为“块级”元素:span默认display属性值 ...
- android 多行 RadioButton的使用
最近项目用到了多行RadioButton,随记录下. 先给出RadioButton的布局 <com.kuibu.jucai.widget.MyRadioGroup android:id=&quo ...
- 这可能是国内最全面的char RNN注释
char RNN代码来源于https://github.com/hzy46/Char-RNN-TensorFlow 前言 本人在学习char RNN的过程中,遇到了很多的问题,但是依然选择一行代码一行 ...
- NOIP2008复赛 提高组 第一题
描述 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大! 这种方法的具体描述如下:假设maxn是单词中出现次数最多的 ...
- Java中各种引用(Reference)解析
目录 1,引用类型 2, FinalReference 2.1, Finalizer 3, SoftReference 4, WeakReference 5, PhantomReference 6, ...
- 将dos格式文件转换为unix格式
在windows下换行符是\r\n,表示回到行首并换到下一行 而unix系统中换行符是\n 这样就存在一个问题,在windows上的文档到了unix上可能就无法使用了 针对这个情况有几种解决办法: 1 ...
- python常用内建模块——datetime
datetime是python处理日期和时间的标准库. 获取当前日期和时间 >>>from datetime import datetime >>>now = da ...
- 52 (OC)* 苹果手机各种尺寸详细表以及iPhoneX、iPhoneXS、iPhoneXR、iPhoneXSMax屏幕适配
iPhone设备 物理分辨率是硬件所支持的,逻辑分辨率是软件可以达到的. 代数 设备 操作系统 逻辑分辨率(point) 物理分辨率(pixel) 屏幕尺寸(对角线长度) 缩放因子 iPhone ...