Monkey King

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6667    Accepted Submission(s):
2858

Problem Description

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.

 

Input

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.

 

Output

For each of the conflict, output -1 if the two monkeys
know each other, otherwise output the strongness value of the strongest monkey
in all friends of them after the duel.
 

Sample Input

5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
 

Sample Output

8 5 5 -1 10
 

分析

左偏树,对于每次操作,取出最大的元素,将它从删除(即合并他的两个子树),除2后在加入进去。然后操作完成后,将两棵树合并即可。

code

 #include<cstdio>
#include<algorithm>
#include<cstring> using namespace std; const int N = ; int val[N],dis[N],ls[N],rs[N],fa[N]; inline void read(int &x) {
x = ;int f = ;char ch = getchar();
for (; ch<''||ch>''; ch = getchar()) if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = getchar()) x = x * + ch - '';
x = x * f;
}
int merge(int x,int y) {
if (!x || !y) return x + y;
if (val[x]<val[y]||(val[x]==val[y]&&x<y)) swap(x,y);
rs[x] = merge(rs[x],y);
fa[rs[x]] = x;
if (dis[ls[x]] < dis[rs[x]]) swap(rs[x],ls[x]);
if (rs[x]) dis[x] = dis[rs[x]] + ;
else dis[x] = ;
return x;
}
inline int find(int x) {
if (x==fa[x]) return x;
return fa[x] = find(fa[x]);
}
inline int work(int x) {
fa[ls[x]] = ls[x];fa[rs[x]] = rs[x]; //-
int t = merge(ls[x],rs[x]);
ls[x] = rs[x] = ; //-
val[x] /= ;
return merge(x,t);
}
int main() {
int m,n,a,b;
while (~scanf("%d",&n)) {
memset(rs,,sizeof(rs));
memset(ls,,sizeof(ls));
memset(dis,,sizeof(dis));
for (int i=; i<=n; ++i) read(val[i]),fa[i] = i;
read(m);
while (m--) {
read(a),read(b);
int x = find(a),y = find(b);
if (x == y) puts("-1");
else printf("%d\n",val[merge(work(x),work(y))]);
}
}
return ;
}

1512 Monkey King的更多相关文章

  1. 数据结构(左偏树):HDU 1512 Monkey King

    Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  2. hdu 1512 Monkey King 左偏树

    题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...

  3. 【HDOJ】1512 Monkey King

    左偏树+并查集.左偏树就是可合并二叉堆. /* 1512 */ #include <iostream> #include <string> #include <map&g ...

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

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1512 [题目大意] 现在有 一群互不认识的猴子,每个猴子有一个能力值,每次选择两个猴子,挑出他们所 ...

  5. HDU 1512 Monkey King(左偏树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1512 题意: 有n只猴子,每只猴子一开始有个力量值,并且互相不认识,现有每次有两只猴子要决斗,如果认识,就不打了 ...

  6. HDU 1512 Monkey King(左偏树)

    Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each does thi ...

  7. hdu 1512 Monkey King —— 左偏树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...

  8. HDU 1512 Monkey King ——左偏树

    [题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...

  9. HDU 1512 Monkey King

    左偏树.我是ziliuziliu,我是最强的 #include<iostream> #include<cstdio> #include<cstring> #incl ...

随机推荐

  1. tyvj P4877 _1.组合数

    时间限制:1s 内存限制:256MB [问题描述] 从m个不同元素中,任取n(n≤m)个元素并成一组,叫做从m个不同元素中取出n个元素的一个组合:从m个不同元素中取出n(n≤m)个元素的所有组合的个数 ...

  2. DOM操作(二)对元素的操作(创建,追加,删除)

    1 创建新的 HTML 元素 (节点) var divDom=document.createElement('div'); 2 添加新元素到尾部 element.appendChild(para); ...

  3. 1169 传纸条 2008年NOIP全国联赛提高组 个人博客:attack.cf

    1169 传纸条 2008年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond         题目描述 Description 小渊和小轩 ...

  4. 设置Cookie最大存活时间

    Cookie和Session都是由Tomcat自动创建的对象,Cookie的默认最大存活时间是 -1 ,即当浏览器关闭时Cookie就消失了:Session的默认最大存活时间是30分钟. 使用Sess ...

  5. Centos 7 搭建git服务器及使用gitolite控制权限

    一.安装git yum install git git --version #查看git版本 二.升级git(可选,如果之前已经安装git,需要升级git到最新版本) git clone https: ...

  6. python 之正则表达式

    一.正则表达式 首先,我们需要感性的了解下什么是正则表达式,简单的是说“正则表达式”就是一个“表达式”,更准确定义是:“用一个简洁的方法来实现对“一组字符串”的表达式. 最终目的就是实现“一行胜千言” ...

  7. HDU 4055 The King’s Ups and Downs(DP计数)

    题意: 国王的士兵有n个,每个人的身高都不同,国王要将他们排列,必须一高一矮间隔进行,即其中的一个人必须同时高于(或低于)左边和右边.问可能的排列数.例子有1千个,但是最多只算到20个士兵,并且20个 ...

  8. UVA 147 Dollars 刀了(完全背包,精度问题)

    题意:一样是求钱的转换方案数,但是这次单位下降到分,但给的是元为单位的,所以是浮点的,但是固定有两位小数. 思路:数据都放大100倍来计算,去除精度问题,转成整型时要注意精度.即使给的是0.02,乘以 ...

  9. miniLCD12864 16引脚

    显示图片 main.c #include<reg51.h>#include"st7565.h"//---存一个图片--//unsigned char code pic[ ...

  10. JS中作用域和变量提升(hoisting)的深入理解

    作用域(Scoping) javascript作用域之所以迷惑,是因为它程序语法本身长的像C家族的语言.我对作用域的理解是只会对某个范围产生作用,而不会对外产生影响的封闭空间.在这样的一些空间里,外部 ...