zoukankan      html  css  js  c++  java
  • 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次架,每次打架呢,都会拉上自己朋友最牛叉的出来跟别人打,打完之后战斗力就会减半,每次打完架就会成为朋友(正所谓不打不相识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.

    有多组数据

    输出格式:

    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.

    输入输出样例

    输入样例#1:
    5
    20
    16
    10
    10
    4
    5
    2 3
    3 4
    3 5
    4 5
    1 5
    
    输出样例#1:
    8
    5
    5
    -1
    10
    

    说明

    题目可能有多组数据

    思路:左偏堆

    代码实现

     1 #include<cstdio>
     2 #include<cstring>
     3 const int maxn=1e5+10;
     4 int n,m;
     5 int a,b,c,d;
     6 int s[maxn],f[maxn],t[maxn],l[maxn],r[maxn];
     7 inline int swap_(int&x,int&y){x^=y,y^=x,x^=y;}
     8 int ff(int x){return f[x]==x?x:f[x]=ff(f[x]);}
     9 int merger(int x,int y){
    10     if(!x) return y;
    11     if(!y) return x;
    12     if(s[x]<s[y]) swap_(x,y);
    13     r[x]=merger(r[x],y);
    14     if(t[r[x]]>t[l[x]]) swap_(r[x],l[x]);
    15     t[x]=t[r[x]]+1;
    16     return x;
    17 }
    18 int main(){
    19     while(scanf("%d",&n)!=EOF){
    20         memset(s,0,sizeof(s));
    21         memset(f,0,sizeof(f));
    22         memset(t,0,sizeof(t));
    23         memset(l,0,sizeof(l));
    24         memset(r,0,sizeof(r));
    25         for(int i=1;i<=n;i++) scanf("%d",&s[i]),f[i]=i;
    26         scanf("%d",&m);
    27         while(m--){
    28             scanf("%d%d",&a,&b);
    29             a=ff(a),b=ff(b);
    30             if(a==b) puts("-1");
    31             else{
    32                 s[a]/=2,s[b]/=2;
    33                 c=f[l[a]]=f[r[a]]=merger(l[a],r[a]);
    34                 d=f[l[b]]=f[r[b]]=merger(l[b],r[b]);
    35                 l[a]=r[a]=l[b]=r[b]=0;
    36                 a=f[a]=f[c]=merger(a,c);
    37                 b=f[b]=f[d]=merger(b,d);
    38                 f[a]=f[b]=merger(a,b);
    39                 printf("%d
    ",s[f[a]]);
    40             }
    41         }
    42     }
    43     return 0;
    44 }

    我为什么只写了合并操作,才不是因为lazy(mmjs),只是为了更好地理解左偏树的性质。

  • 相关阅读:
    几个用于序列化的代码片段
    一个统计查询写的哥要哭了
    pb数据窗口数据输入的下拉选择效果
    PB代码块
    获取指定表的字段信息sql2005
    TSQL时间格式化显示
    Atitit 为什么互联网金融会得到高层的支持 面子工程战略 政绩战略 大事业战略 高层需要在意识形态创新全球,政绩工程 得到合法性。 银行有很多家,而且别的区域也有。。不独特。。但是支付
    Atitit 软件与互联网理论 attilax总结
    Atitit 软件与开发的未来趋势 attilax总结 1.1. Sdx软件重构世界 软件定义未来 1 1.2. 《软件和信息技术服务业发展规划(20162020年)》( 2 1.3. Iot物联
    Atitit it业界与软件界的定律 原则 准则 法则 效应
  • 原文地址:https://www.cnblogs.com/J-william/p/6970251.html
Copyright © 2011-2022 走看看