Money King【题解】
我又傻了……竟然忘了区别大根堆和小根堆的性质,以至于一个符号打错,debug了半天……(我真是太菜了……)
题目描述
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.
有多组数据
输入样例:
输出样例:
-
解:
每次打完架成为朋友,告诉了我们要合并;查找两人是不是朋友,告诉我们要用路径压缩并查集;找最大值,告诉了我们要用大根堆。
这题做完了。注意merge的符号,以及多重数据的提醒。
题目中要求把堆顶的值减半。然而不方便直接减,于是我们先把它与它的子树断掉,然后合并它的两颗子树,再把它合并进去即可。
注意合并两个堆,最终要把它们全部合并并且输出堆顶。
Code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#define ll long long
#define lc tr[x].l
#define rc tr[x].r
using namespace std;
const int MAXN=;
inline void swap(int &x,int &y){x^=y^=x^=y;}
inline int read(){
char ch=getchar();
int s=,w=;while(ch<''||ch>''){if(ch=='-')w=-;ch=getchar();}
while(ch>=''&&ch<=''){s=(s<<)+(s<<)+(ch^);ch=getchar();}return s*w;
}
struct node{
int l,r,dis,w,fa;
}tr[MAXN];
int merge(int x,int y){
if(!x||!y)return x+y;
if(tr[x].w<tr[y].w||(tr[x].w==tr[y].w&&x<y))swap(x,y);
rc=merge(rc,y);tr[rc].fa=tr[lc].fa=x;
if(tr[lc].dis<tr[rc].dis)swap(lc,rc);tr[x].dis=tr[rc].dis+;
return x;
}
inline int getf(int x){return x==tr[x].fa?x:tr[x].fa=getf(tr[x].fa);}
void work(int x,int y){
x=getf(x),y=getf(y);
if(x==y){
puts("-1");
return;
}
int rt,art,brt;
tr[x].w>>=;
rt=merge(tr[x].l,tr[x].r);
tr[x].l=tr[x].r=tr[x].dis=;
art=merge(rt,x);
tr[rt].fa=tr[x].fa=art;
tr[y].w>>=;
rt=merge(tr[y].l,tr[y].r);
tr[tr[y].l].fa=tr[tr[y].r].fa=rt;
tr[y].l=tr[y].r=tr[y].dis=;
brt=merge(rt,y);
tr[rt].fa=tr[y].fa=brt;
rt=merge(art,brt);
tr[art].fa=tr[brt].fa=rt;
printf("%d\n",tr[rt].w);
}int n,m;
int main(){
while(~scanf("%d",&n)){
tr[].dis=-;
for(register int i=;i<=n;i++){
tr[i].fa=i;
tr[i].w=read();
tr[i].dis=tr[i].l=tr[i].r=;
}m=read();
for(register int i=;i<=m;i++)work(read(),read());
}
return ;
}
Money King【题解】的更多相关文章
- POJ2728:Desert King——题解
http://poj.org/problem?id=2728 题目大意:求一棵生成树使得路费用和/路长之和最小(路的费用是两端点的高度差) 最小比率生成树. 我们还是01分数规划的思想将边权变为路费用 ...
- DP——由蒟蒻到神犇的进阶之路
开始更新咯 DP专题[题目来源BZOJ] 一.树形DP 1.bzoj2286消耗战 题解:因为是树形结构,一个点与根节点不联通,删一条边即可, 于是我们就可以简化这棵树,把有用的信息建立一颗虚树,然后 ...
- POJ 1904 King's Quest(强连通图)题解
题意:n个王子有自己喜欢的ki个公主,有n个公主,每个王子只能娶一个自己喜欢的公主且不能绿别的王子.现在给你一种王子娶公主的方案,并且保证这种方案是正确的.请你给出,每个王子能娶哪些公主,要求娶这些公 ...
- 【luogu P1456 Monkey King】 题解
题目链接:https://www.luogu.org/problemnew/show/P1456 左偏树并查集不加路径压缩吧... #include <cstdio> #include & ...
- LuoguP7041 [NWRRC2016]King's Heir 题解
Content 给出现在的日期,请从 \(n\) 个人当中选出一个人,使得他是所有成年人(\(\geqslant 18\) 岁的人)中年龄最小的. 数据范围:设日期为 \(yy/mm/dd\),则有 ...
- BZOJ-1087 互不侵犯King 状压DP+DFS预处理
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- cdoj 93 King's Sanctuary 傻逼几何题
King's Sanctuary Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/sho ...
- hdu 5640 King's Cake(BestCoder Round #75)
King's Cake Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
随机推荐
- 一个字符带下滑线的EditText
效果样式: 这个比较特别的editText是公司的一个新的需求,我也是在网上找了一下,然后看到了一篇博客然后修改成自己需要的样式.这种一般的思路就是在onDraw()方法绘制editText的特别的样 ...
- 如何在Android studio上运行从github上下载的RN项目
想要编译别人的RN项目,还是要踩踩坑才能走上正轨啊,分享下我试过多种方法后最喜欢的方法(其实是因为我多次用VS Code编译都是以失败而告终,所以才选择的studio) 注意:这一步是你的开发环境都安 ...
- 智能指针std::unique_ptr
std::unique_ptr 1.特性 1) 任意时刻只能由一个unique_ptr指向某个对象,指针销毁时,指向的对象也会被删除(通过内置删除器,通过调用析构函数实现删除对象) 2)禁止拷贝和赋值 ...
- selenium-测试框架搭建(十三)
思路 分离业务代码和测试数据,提高代码可维护性,实现自动化,减少重复劳动. 一个测试框架大概由配置文件,测试数据,测试用例,相关文件(发送邮件等),测试日志,断言和测试报告等模块组成. 结构 以页面为 ...
- sqlserver 2014使用时有Cannot find one or more components
好久没用sqlserver,今天打开却出现了一个错误,Cannot find one or more components,令人头疼.在启动Microsoft SQL Server Managemen ...
- [RHEL 7]ISCSI服务端及客户端连接配置
环境RHEL7.4 1.搭建服务器端主机环境 网络配置 网卡eth0 10.0.0.1 网卡eth1 10.1.0.1 网卡eth2 10.2.0.1 网卡eth3 10.3.0.1 硬盘配置 添加一 ...
- Azure按订阅,资源组,资源类型导出所有资源powershell命令
一般可以借助powershell命令来读取资源:例如:1, 读取某个订阅下的资源:$subscriptionID = "xxxxxxxx"Set-AzureRmContext $s ...
- FB商务管理平台(Business Manager) (2)
Business Manager 商务管理平台(以下简称BM)API 一站式管理广告帐户.主页及相关的工作人员. BM功能结构(其中:账户下的节点属于市场营销API) API / SDK FB提供了多 ...
- C: printf参数执行顺序与前置后置自增自减的影响
起源: 今天在了解副作用side-effect的过程中,看到了下面的网页,把我带到了由printf引起的一系列问题,纠结了一整天,勉强弄懂. 第一个代码没什么好解释的.而第二个printf(" ...
- linux系统docker版本升级或安装
如果存在旧版本,则先卸载 最好先将镜像导出保存,以免升级后丢失或者无法使用 如有正在运行的容器,先停止 $ docker ps -q | xargs docker stop 关闭docker服务 $ ...