题目地址:P1456 Monkey King

一道挺模板的左偏树题

不会左偏树?看论文模板,完了之后再回来吧

然后你发现看完论文打完模板之后就可以A掉这道题不用回来了

细节见代码

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 6;
int n, m, f[N], a[N], l[N], r[N], d[N];

//类并查集路径压缩
int get(int x) {
    if (x == f[x]) return x;
    return f[x] = get(f[x]);
}

//左偏树合并
inline int merge(int x, int y) {
    if (!x || !y) return x + y;//x或y为0则返回另一个的简写
    if (a[x] < a[y]) swap(x, y);//保证堆性质
    r[x] = merge(r[x], y);
    f[r[x]] = x;
    if (d[l[x]] < d[r[x]]) swap(l[x], r[x]);//保证左偏树性质
    d[x] = d[r[x]] + 1;
    return x;
}

//删除堆顶,注意语句顺序
inline void pop(int x) {
    f[l[x]] = l[x], f[r[x]] = r[x];
    f[x] = merge(l[x], r[x]);
    l[x] = r[x] = 0;
}

//多组数据单独写一个函数
inline void work() {
    d[0] = -1;//0的“距离”为-1
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        f[i] = i;
        d[i] = l[i] = r[i] = 0;//多组数据清空数组
    }
    cin >> m;
    while (m--) {
        int x, y;
        scanf("%d %d", &x, &y);
        int fx = get(x), fy = get(y);//找堆顶
        if (fx == fy) puts("-1");//若在同一个堆中输出-1
        else {
            pop(fx), pop(fy);//删除堆顶
            a[fx] >>= 1, a[fy] >>= 1;
            f[fx] = merge(fx, f[fx]), f[fy] = merge(fy, f[fy]);//将新值插入堆顶
            fx = get(fx), fy = get(fy);
            printf("%d\n", a[merge(fx,fy)]);//输出堆顶,注意堆中存的是下标而不是数
        }
    }
}

int main() {
    while (cin >> n) work();
    return 0;
}

P1456 Monkey King的更多相关文章

  1. 洛谷P1456 Monkey King

    https://www.luogu.org/problemnew/show/1456 #include<cstdio> #include<iostream> #include& ...

  2. 【luogu P1456 Monkey King】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1456 左偏树并查集不加路径压缩吧... #include <cstdio> #include & ...

  3. ZOJ 2334 Monkey King

    并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子                       Monkey King ...

  4. 数据结构(左偏树):HDU 1512 Monkey King

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

  5. HDU - 5201 :The Monkey King (组合数 & 容斥)

    As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...

  6. Monkey King(左偏树 可并堆)

    我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...

  7. 1512 Monkey King

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

  8. The Monkey King(hdu5201)

    The Monkey King Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  9. hdu1512 Monkey King

    Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each ...

随机推荐

  1. RJ45连接器

    http://www.huilyn.com/path315.html      HBJ-6308ANLF http://www.hanrun.com/en/           HR971169C h ...

  2. mysql视图、触发事务、存储过程

    视图 视图是一个虚拟表(非真实存在的),其本质就是根据SQL语言获取动态的数据集,并为其命名,用户使用时只需要使用名称即可获得结果集,可以将结果集当做表来使用. 视图是存在数据库中的,如果我们程序中使 ...

  3. matlab : Nelder mead simplex 单纯形直接搜索算法;

    function [ param ] = NeldSearch( param ) %NERDSEARCH 此处显示有关此函数的摘要 % nelder mead simplex 单纯形直接搜索算法: % ...

  4. Uart串口

    title: Uart串口 tags: ARM date: 2018-10-20 16:38:28 --- Uart串口 和单片机的应用没什么区别,首先设置IO复用,设置波特率和数据位,以及中断相关的 ...

  5. Numpy 笔记: 多维数组的切片(slicing)和索引(indexing)【转】

    目录 切片(slicing)操作 索引(indexing) 操作 最简单的情况 获取多个元素 切片和索引的同异 切片(slicing)操作 Numpy 中多维数组的切片操作与 Python 中 lis ...

  6. POJ - 2057 The Lost House(树形DP+贪心)

    https://vjudge.net/problem/POJ-2057 题意 有一只蜗牛爬上某个树枝末睡着之后从树上掉下来,发现后面的"房子"却丢在了树上面,.现在这只蜗牛要求寻找 ...

  7. Spark源码剖析 - 任务提交与执行

    1. 任务概述 任务提交与执行过程: 1) build operator DAG:此阶段主要完成RDD的转换及DAG的构建: 2) split graph into stages of tasks:此 ...

  8. VS Code +node npm 调试 js

    打开vsCode的调试控制台里面的终端 然后输入下面代码 npm install express-generator -g 1 创建一个命名为 myapp 的应用. express myapp 你就可 ...

  9. solr集群SolrCloud(solr+zookeeper)windows搭建

    SolrCloud是什么 参考 solrCloud官网介绍 http://lucene.apache.org/solr/guide/6_6/solrcloud.html Apache Solr 可以设 ...

  10. Django相关面试题

    Django框架的生命请求周期 浏览器上输入地址,回车然后发生了什么? => Http请求生命周期 ? 什么是wsgi 以及作用? 中间件 中间件的执行流程? 中间件的执行流程? 说一下Djan ...