题意:

猴子们打架  认识的猴子不会打架  两仅仅猴子打完以后就认识了  A认识B B认识C A也认识C  每次打架由两伙猴子进行  分别选出自己的最高战斗力  在战斗之后两仅仅猴子战斗力减半  给出m次打架  输出打架后这一伙猴子里的最强战斗力

思路:

推断两仅仅猴子是不是一伙的  用到并查集

高速找出一伙猴子中的最强战斗力用到堆  但打完架两伙猴子合并时堆须要nlogn复杂度  因此用左偏树取代堆

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010 int n,m,tot;
int fa[N];
struct node
{
int v,l,r,dis;
}nd[N]; int getf(int x)
{
if(x!=fa[x]) fa[x]=getf(fa[x]);
return fa[x];
} int newnode(int x)
{
nd[tot].v=x;
nd[tot].l=0;
nd[tot].r=0;
nd[tot].dis=0;
tot++;
return tot-1;
} int merge(int x,int y)
{
if(!x) return y;
if(!y) return x;
if(nd[x].v<nd[y].v) swap(x,y);
nd[x].r=merge(nd[x].r,y);
if(nd[nd[x].l].dis<nd[nd[x].r].dis) swap(nd[x].l,nd[x].r);
nd[x].dis=nd[nd[x].r].dis+1;
return x;
} int pop(int x)
{
int tmp=merge(nd[x].l,nd[x].r);
nd[x].dis=nd[x].l=nd[x].r=0;
return tmp;
} int main()
{
int i,j,fi,fj,x;
while(~scanf("%d",&n))
{
tot=1;
for(i=1;i<=n;i++)
{
scanf("%d",&j);
fa[i]=newnode(j);
}
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&i,&j);
fi=getf(i);
fj=getf(j);
if(fi==fj) puts("-1");
else
{
i=pop(fi);
nd[fi].v/=2;
j=pop(fj);
nd[fj].v/=2;
i=merge(i,j);
i=merge(i,fi);
i=merge(i,fj);
fa[i]=fa[fi]=fa[fj]=i;
printf("%d\n",nd[i].v);
}
}
}
return 0;
}

ZOJ 2334 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

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

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

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

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

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

  5. hdu 1512 Monkey King —— 左偏树

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

  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 ——左偏树

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

  8. HDU 1512 Monkey King

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

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

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

随机推荐

  1. 【Nutch2.2.1基础教程之6】Nutch2.2.1抓取流程 分类: H3_NUTCH 2014-08-15 21:39 2530人阅读 评论(1) 收藏

    一.抓取流程概述 1.nutch抓取流程 当使用crawl命令进行抓取任务时,其基本流程步骤如下: (1)InjectorJob 开始第一个迭代 (2)GeneratorJob (3)FetcherJ ...

  2. Lucene学习总结之一:全文检索的基本原理 2014-06-25 14:11 666人阅读 评论(0) 收藏

    一.总论 根据http://lucene.apache.org/java/docs/index.html 定义: Lucene 是一个高效的,基于Java 的全文检索库. 所以在了解Lucene之前要 ...

  3. 使用Html5开发Android和iOS应用:HBuilder、Html5Plus、MUI

    活动主题:五一巨献,问答有礼,105QB送给IT互联网界的劳动人民活动时间:4月30日晚上10点~5月2日晚上10点网址: http://ask.jiutianniao.com  2014年的时候,就 ...

  4. POST提交数据时四种常见的数据格式

    最近项目部署到新环境tomcat+mysql,想看看项目部署成功没有,就用soupui调对应接口开测试,soupui使用比较简单,给上接口地址,入参xml报文,把入参的media Type设置为app ...

  5. [Ramda] Compose lenses

    We can compose lenses to get value: const addrs = [{street: '99 Walnut Dr.', zip: '04821'}, {street: ...

  6. [Linux] Search the contents of files using grep

    Learn the basic syntax for using grep to search the contents of a single file or files. It's like CM ...

  7. Multi-Tenancy模式,基础服务大规模扩张的时候,是应该推进了。

    这不是技术介绍.这是我要推进的工作,记在这里.服务的对象太多,必须隔离为不同租户了.

  8. [tmux] Copy and paste text from a tmux session

    One non-obvious, but extremely useful, feature in tmux is copy-pasting text between panes. This also ...

  9. 使用JSONP解决跨域问题-代码示例

    前段时间用JSONP解决了跨域问题,现在不用了,把代码思路记下来,今后说不定还用得上. JS代码 //查询公告数据 function recentpost(){ $.getJSON(cmsUrl+&q ...

  10. ie7span标签float换行悬浮

    项目中,ie8以上都是好好的,就是ie7的布局有问题,span换行漂浮了 <div style="height:20px"> <span style=" ...