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),只是为了更好地理解左偏树的性质。

  • 相关阅读:
    逻辑卷扩容
    iptables网络防火墙||SNAT,DNAT等转发路由动作
    常用rman操作语句
    常用Oracle的SQL语句
    oracle中的单引号和双竖线||以及q'间隔符
    网站升级HTTPS教程
    站长必备:网站被黑后怎么快速搞定
    常见HTTP错误代码
    在Android中实现一个简易的Http服务器
    Android开发新手常见的10个误区
  • 原文地址:https://www.cnblogs.com/J-william/p/6970251.html
Copyright © 2011-2022 走看看