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.
 
题目大意:一开始有n只孤独的猴子,然后他们要打m次架,每次打架呢,都会拉上自己朋友最牛叉的出来跟别人打,打完之后战斗力就会减半,每次打完架就会成为朋友(正所谓不打不相识o(∩_∩)o )。问每次打完架之后那俩猴子最牛叉的朋友战斗力还有多少,若朋友打架就输出-1.
思路:需要的操作有,选出最牛叉的猴子,合并两堆猴子,让最牛逼的猴子战力减半,比较合适的数据结构就是左偏树啦~
 
代码(781MS):
 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = ; int n, m;
int fa[MAXN]; int key[MAXN], child[MAXN][], dist[MAXN];
int stk[MAXN], top, node_cnt;
int root[MAXN]; void init() {
dist[] = -;
top = node_cnt = ;
} int newNode(int k) {
int x = top ? stk[top--] : ++node_cnt;
dist[x] = ; key[x] = k;
child[x][] = child[x][] = ;
return x;
} void maintain(int &x) {
if(dist[child[x][]] < dist[child[x][]])
swap(child[x][], child[x][]);
dist[x] = dist[child[x][]] + ;
} int merge(int &x, int &y) {
if(x == ) return y;
if(y == ) return x;
if(key[y] > key[x]) swap(x, y);
child[x][] = merge(child[x][], y);
maintain(x);
return x;
} int del(int &x) {
if(x != ) {
stk[++top] = x;
return merge(child[x][], child[x][]);
}
return ;
} int getfather(int x) {
return fa[x] == x ? x : fa[x] = getfather(fa[x]);
} int merge2(int x, int y) {
return fa[x] = y;
} void solve(int u, int v) {
int fu = getfather(u);
int fv = getfather(v);
if(fu == fv) {
printf("-1\n");
return ;
}
int p1 = newNode(key[root[fu]] / );
int p2 = newNode(key[root[fv]] / );
int p3 = del(root[fu]);
int p4 = del(root[fv]);
p3 = merge(p1, p3);
p4 = merge(p2, p4);
int x = merge2(fu, fv);
root[x] = merge(p3, p4);
printf("%d\n", key[root[x]]);
} int main() {
int k, u, v;
while(scanf("%d", &n) != EOF) {
init();
for(int i = ; i <= n; ++i) {
scanf("%d", &k);
root[i] = newNode(k);
fa[i] = i;
}
scanf("%d", &m);
while(m--) {
scanf("%d%d", &u, &v);
solve(u, v);
}
}
}

使用pb_ds库。使用方法可以参考WC2015的论文《C++的pb_ds库在OI中的应用》。

下面代码使用的Tag为pairing_heap_tag。

此外,binomial_heap_tag为982MS,rc_binomial_heap_tag直接MLE了。

代码(858MS):

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <ext/pb_ds/priority_queue.hpp> using __gnu_pbds::priority_queue; const int MAXN = ; priority_queue<std::pair<int, int>, std::less<std::pair<int, int> >, __gnu_pbds::pairing_heap_tag> root[MAXN];
int fa[MAXN];
int n, m; int get_set(int x) {
return fa[x] == x ? x : fa[x] = get_set(fa[x]);
} int solve(int u, int v) {
int fu = get_set(u), fv = get_set(v);
if(fu == fv) return -;
std::pair<int, int> best_u = root[fu].top(); root[fu].pop();
std::pair<int, int> best_v = root[fv].top(); root[fv].pop();
root[fu].push(std::make_pair(best_u.first / , best_u.second));
root[fv].push(std::make_pair(best_v.first / , best_v.second));
root[fu].join(root[fv]); fa[fv] = fu;
return root[fu].top().first;
} int main() {
int k, u, v;
while(scanf("%d", &n) != EOF) {
for(int i = ; i <= n; ++i) {
scanf("%d", &k);
root[i].clear();
root[i].push(std::make_pair(k, i));
fa[i] = i;
}
scanf("%d", &m);
while(m--) {
scanf("%d%d", &u, &v);
printf("%d\n", solve(u, v));
}
}
}

HDU 1512 Monkey King(左偏树)的更多相关文章

  1. hdu 1512 Monkey King 左偏树

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

  2. hdu 1512 Monkey King —— 左偏树

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

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

    题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...

  4. HDU 1512 Monkey King ——左偏树

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

  5. HDU 1512 Monkey King(左偏堆)

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

  6. ZOJ2334 Monkey King 左偏树

    ZOJ2334 用左偏树实现优先队列最大的好处就是两个队列合并可以在Logn时间内完成 用来维护优先队列森林非常好用. 左偏树代码的核心也是两棵树的合并! 代码有些细节需要注意. #include&l ...

  7. zoj 2334 Monkey King/左偏树+并查集

    原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...

  8. HDU1512 ZOJ2334 Monkey King 左偏树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - ZOJ2334 题目传送门 - HDU1512 题意概括 在一个森林里住着N(N<=10000)只猴子. ...

  9. hdu1512 Monkey King(左偏树 + 并查集)

    Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...

  10. LuoguP1456 Monkey King (左偏树)

    struct LeftTree{ int l,r,val,dis; }t[N]; int fa[N]; inline int Find(int x){ return x == fa[x] ? x : ...

随机推荐

  1. 笔记: 对称加密算法的PKCS5 和 PKCS7 填充

    PKCS #7 填充字符串由一个字节序列组成,每个字节填充该填充字节序列的长度. 假设,块的长度是 8, 数据长度是 5 数据:AA AA AA AA AA PKCS#7 填充 AA AA AA AA ...

  2. Oracle批量删除表格数据

    在开发阶段往Oracle数据库中多个表格中导入了许多测试数据,倘若一张张表执行"truncate table tablename"语句显得十分繁琐.在PL/SQL中可以用代码进行批 ...

  3. Linux-帮助的用法

    Linux帮助使用方法 内部命令:help COMMAND 或 man bash外部命令: (1) COMMAND --help   COMMAND -h --help和-h选项显示用法总结和参数列表 ...

  4. ON DUPLICATE KEY UPDATE 插入or更新

    mean:若数据表中存在以相同主键的记录,就更新该条记录.否则就插入一条新的记录. 单条:INSERT INTO tablename (`field1`,`field2`) VALUES(value1 ...

  5. 如何在HHDI中进行数据质量探查并获取数据剖析报告

    通过执行多种数据剖析规则,对目标表(或一段SQL语句)进行数据质量探查,从而得到其数据质量情况.目前支持以下几种数据剖析类型,分别是:数字值分析.值匹配检查.字符值分析.日期值分析.布尔值分析.重复值 ...

  6. STM32(9)——通用定时器作为输入捕捉

    通用定时器作为输入捕获的使用.我们将用 TIM5 的通道 1 (PA0)来做输入捕获,捕获 PA0 上高电平的脉宽(用 WK_UP 按键输入高电平),通过串口打印高电平脉宽时间 输入捕获简介 输入捕获 ...

  7. Python 爬虫 招聘信息并存入数据库

    新学习了selenium,啪一下腾讯招聘 from lxml import etree from selenium import webdriver import pymysql def Geturl ...

  8. 论文翻译第二弹--用python(或Markdown)对论文复制文本进行处理

    图中这种论文你想进行文本复制放入翻译软件进行翻译时,会发现是这种形式: 句子之间是断开的,这时普遍的方法,也是我之前一直用的方法就是打开一个文档编辑器,复制上去后一行行地继续调整.昨天不想这样了,就打 ...

  9. go基础语法-条件语句

    1.if else 语句 if语句后面的条件不需要括号 if n > 0 { return 1 }else { return -1 } 'if'之后,条件判断之前,可以初始化变量(作用域为整个i ...

  10. 【blockly教程】第二章 Blockly编程基础

    2.1 Blockly的数据类型 2.1.1 数据的含义  在计算机程序的世界里,程序的基本任务就是处理数据,无论是数值还是文字.图像.图形.声音.视频等信息,如果要在计算机中处理的话,就必须将它们转 ...