zoukankan      html  css  js  c++  java
  • 【BZOJ 1455】 1455: 罗马游戏 (可并堆-左偏树+并查集)

    1455: 罗马游戏

    Description

    罗马皇帝很喜欢玩杀人游戏。 他的军队里面有n个人,每个人都是一个独立的团。最近举行了一次平面几何测试,每个人都得到了一个分数。 皇帝很喜欢平面几何,他对那些得分很低的人嗤之以鼻。他决定玩这样一个游戏。 它可以发两种命令: 1. Merger(i, j)。把i所在的团和j所在的团合并成一个团。如果i, j有一个人是死人,那么就忽略该命令。 2. Kill(i)。把i所在的团里面得分最低的人杀死。如果i这个人已经死了,这条命令就忽略。 皇帝希望他每发布一条kill命令,下面的将军就把被杀的人的分数报上来。(如果这条命令被忽略,那么就报0分)

    Input

    第一行一个整数n(1<=n<=1000000)。n表示士兵数,m表示总命令数。 第二行n个整数,其中第i个数表示编号为i的士兵的分数。(分数都是[0..10000]之间的整数) 第三行一个整数m(1<=m<=100000) 第3+i行描述第i条命令。命令为如下两种形式: 1. M i j 2. K i

    Output

    如果命令是Kill,对应的请输出被杀人的分数。(如果这个人不存在,就输出0)

    Sample Input

    5
    100 90 66 99 10
    7
    M 1 5
    K 1
    K 1
    M 2 3
    M 3 4
    K 5
    K 4

    Sample Output

    10
    100
    0
    66

    HINT

    部分数据如下 JudgeOnline/upload/201607/aa.rar

     
     
    【分析】
      左偏树裸题233
      自己打的啦没有抄了。。
      中间要用并查集哦。。
     
     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm>
     6 using namespace std;
     7 #define Maxn 1000010
     8 
     9 struct node
    10 {
    11     int x,lc,rc,dis;
    12 }t[Maxn];
    13 
    14 void upd(int x)
    15 {
    16     t[x].lc=t[x].rc=t[x].dis=0;
    17 }
    18 
    19 int rt[Maxn],a[Maxn];
    20 int rtt(int x)
    21 {
    22     if(rt[x]!=x) rt[x]=rtt(rt[x]);
    23     return rt[x];
    24 }
    25 
    26 struct Ltree
    27 {
    28     int merge(int x,int y)
    29     {
    30         if(x==0||y==0) return x+y;
    31         if(t[x].x>t[y].x) swap(x,y);
    32         t[x].rc=merge(t[x].rc,y);
    33         if(t[t[x].lc].dis<t[t[x].rc].dis) swap(t[x].lc,t[x].rc);
    34         t[x].dis=t[t[x].rc].dis+1;
    35         // rt[t[x].rc]=x;
    36         return x;
    37     }
    38 }heap;
    39 
    40 char s[110];
    41 bool mark[Maxn];
    42 
    43 int main()
    44 {
    45     int n;
    46     scanf("%d",&n);
    47     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    48     for(int i=1;i<=n;i++) rt[i]=i,upd(i),t[i].x=a[i],mark[i]=1;
    49     int m;
    50     scanf("%d",&m);
    51     for(int i=1;i<=m;i++)
    52     {
    53         scanf("%s",s);
    54         if(s[0]=='M')
    55         {
    56             int x,y;
    57             scanf("%d%d",&x,&y);
    58             if(!mark[x]||!mark[y]) continue;
    59             if(rtt(x)==rtt(y)) continue;
    60             int nw=heap.merge(rtt(x),rtt(y));
    61             rt[rtt(x)]=rt[rtt(y)]=nw;
    62         }
    63         else
    64         {
    65             int x,nw;
    66             scanf("%d",&x);
    67             if(!mark[x]) {printf("0
    ");continue;}
    68             nw=rtt(x);
    69             int xx=heap.merge(t[nw].lc,t[nw].rc);
    70             rt[xx]=xx;rt[nw]=xx;
    71             printf("%d
    ",t[nw].x);
    72             mark[nw]=0;
    73         }
    74     }
    75     return 0;
    76 }
    View Code

    2017-01-18 10:29:39

  • 相关阅读:
    objectc中NSString字符串匹配操作
    objectc中字符串操作的集合
    ios学习地址
    objectc中数组的排序
    转载 Debugging tips for ObjectiveC programming
    NSMutableArray的函数图
    objectc中NSString 路径操作
    object-c中对文件和url操作
    用VS向SharePoint中部署自定义的Content Type
    利用JQuery的ColorBox实现PopUp的功能
  • 原文地址:https://www.cnblogs.com/Konjakmoyu/p/6295778.html
Copyright © 2011-2022 走看看