洛谷P1456Monkey King
洛谷P1456 Monkey King
题目描述
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次架,每打一次架,双方都会派出自己一方战斗力最强的猴子参战,打完架后,所派出的猴子的战斗力减半,同时两方猴子变为朋友,如果友方内部发生冲突,输出-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.
输入输出样例
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
8
5
5
-1
10 这是一道左偏树的模板题,我发现单靠并查集是做不了的,于是去了解了一下左偏树这个东西。
打架前,先判断两只猴子是不是属于同一方,若是,输出-1;若不是,直接用左偏树的merge函数合并两方猴子,同时维护左偏树,输出根节点即可。
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int n,m,x1,y;
int f1,f2,u,v;
struct node{
int r;
int l;
int v;
int dis;
int f;
}tr[];
int find(int x)
{
if(tr[x].f!=x) return find(tr[x].f);
return x;
}
int merge(int a,int b)//合并两方猴子
{
if(a==) return b;
if(b==) return a;
if(tr[a].v<tr[b].v) swap(a,b);//性质1:节点的键值小于或等于它的左右子节点的键值。
tr[a].r=merge(tr[a].r,b);//性质2:节点的左子节点右子节点也是一颗左偏树。
tr[tr[a].r].f=a;
if(tr[tr[a].l].dis<tr[tr[a].r].dis) swap(tr[a].l,tr[a].r);//性质:3:节点的左子节点的距离不小于右子节点的距离。
if(tr[a].r==) tr[a].dis=;
else tr[a].dis=tr[tr[a].r].dis+;
return a;
}
int pop(int a)
{
int l=tr[a].l;
int r=tr[a].r;
tr[l].f=l;
tr[r].f=r;
tr[a].l=tr[a].r=tr[a].dis=;
return merge(l,r);
}
int main()
{
while(scanf("%d",&n)!=EOF)//多组数据
{
for(int i=;i<=n;i++)
{
scanf("%d",&tr[i].v);
tr[i].l=tr[i].r=tr[i].dis=;
tr[i].f=i;
}
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&x1,&y);
f1=find(x1);
f2=find(y);
if(f1==f2)
printf("-1\n");
else
{
tr[f1].v/=;
u=pop(f1);
u=merge(u,f1);
tr[f2].v/=;
v=pop(f2);
v=merge(v,f2);
printf("%d\n",tr[merge(u,v)].v);
}
}
}
return ;
}
洛谷P1456Monkey King的更多相关文章
- 洛谷 P1456Monkey King
题目描述 要把打架的两堆猴子合并为一堆,查询的又是最大值,所以很容易想到可并堆. 题目要求打完架后战斗力最大的猴子的战斗力要减半,但不能直接在堆中进行这个操作,因为战斗力减半后这只猴子不一定是战斗力最 ...
- 洛谷1377 M国王 (SCOI2005互不侵犯King)
洛谷1377 M国王 (SCOI2005互不侵犯King) 本题地址:http://www.luogu.org/problem/show?pid=1377 题目描述 天天都是n皇后,多么无聊啊.我们来 ...
- [洛谷3457][POI2007]POW-The Flood
洛谷题目链接:[POI2007]POW-The Flood 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方 ...
- 【流水调度问题】【邻项交换对比】【Johnson法则】洛谷P1080国王游戏/P1248加工生产调度/P2123皇后游戏/P1541爬山
前提说明,因为我比较菜,关于理论性的证明大部分是搬来其他大佬的,相应地方有注明. 我自己写的部分换颜色来便于区分. 邻项交换对比是求一定条件下的最优排序的思想(个人理解).这部分最近做了一些题,就一起 ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
- 洛谷P1108 低价购买[DP | LIS方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
- 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP
题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...
- 洛谷P1710 地铁涨价
P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交 讨论 题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...
随机推荐
- select 语句中 if 的用法
IF( expr1 , expr2 , expr3 ) expr1 的值为 TRUE,则返回值为 expr2 expr1 的值为FALSE,则返回值为 expr3 例: ,); ,); ", ...
- TableView之表头、表尾,区头、区尾!
一.UITableView的UITableViewStyle 样式分为UITableViewStylePlain和UITableViewStyleGrouped两种: plain样式下区头和区尾是悬浮 ...
- 《DSP using MATLAB》Problem 8.4
今天是六一儿童节,陪伴不了家人,心里思念着他们,看着地里金黄的麦子,远处的山,高高的天 代码: %% ------------------------------------------------- ...
- <每日一题>题目19:简单的程序执行效率面试题
# 将下面的函数按照执行效率高低排序.它们都接受由0至1之间的数字构成的列表作为输入.这个列表可以很长.一个输入列表的示例如下:[random.random() for i in range(1000 ...
- Spring cloud config client获取不到配置中心的配置
Spring cloud client在配置的时候,配置文件要用 bootstrap.properties 贴几个说明的链接.但是觉得说的依然不够详细,得空详查. 链接1 链接2 链接3 原文地址:h ...
- RabbitMQ安装到使用入门
一.安装erlang1.sudo vim /etc/yum.repos.d/rabbitmq-erlang.repo,将如下内容复制粘贴进去后保存:[rabbitmq-erlang]name=rabb ...
- Mybatis的插件 PageHelper 分页查询使用方法
参考:https://blog.csdn.net/ckc_666/article/details/79257028 Mybatis的一个插件,PageHelper,非常方便mybatis分页查询,国内 ...
- Codeforces 500D. New Year Santa Network
题目大意 给你一颗有\(n\)个点的树\(T\),边上有边权. 规定,\(d(i,j)\)表示点i到点j路径上的边权之和. 给你\(q\)次询问,每次询问格式为\(i, j\),表示将按输入顺序排序的 ...
- win10 安装face_recongnition
1.安装dlib https://stackoverflow.com/questions/41912372/dlib-installation-on-windows-10 2.安装face_recon ...
- php filemtime filectime fileatime的区别
1.fileatime()int fileatime(string filename):fileatime()函数返回filename最后访问的时间,这里的最后访问是指每当一个文件的数据块被读取,采用 ...