题目地址: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. ETL过程

    ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract).转换(transform).加载(load)至目的端的过程.ETL一词较常用在数 ...

  2. vue init 解决办法

    /由于windows系统的某方面问题,vue脚手架安装可能会出现第一证书丢失 // 报错:vue-cli · Failed to download repo vuejs-templates/webpa ...

  3. Event Recommendation Engine Challenge分步解析第一步

    一.简介 此项目来自kaggle:https://www.kaggle.com/c/event-recommendation-engine-challenge/ 数据集的下载需要账号,并且需要手机验证 ...

  4. Django中的ORM框架使用小技巧

      Django中的ORM框架使用小技巧 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. Django对各个数据提供了很好的支持,包括PostgreSQL,MySQL,SQLite ...

  5. SearchServer Elasticsearch Cluster / kibana

    S 使用nginx代理kibana并设置身份验证 https://blog.csdn.net/wyl9527/article/details/72598112 使用nginx代理kibana并设置身份 ...

  6. PHP7 网络编程(五)进程间通信【待】

    https://blog.csdn.net/godleading/article/details/78391159

  7. 905. Sort Array By Parity

    Description Given an array A of non-negative integers, return an array consisting of all the even el ...

  8. 面向对象(Object Orientation Programming)

    Three characteristic of object orientation: Encapsulation: capturing data and keeping it safely and ...

  9. vue中axios 配置请求拦截功能 及请求方式如何封装

    main.js 中: import axios from '................/axios' axios.js 中: //axios.js import Vue from 'vue' i ...

  10. XML外部实体注入漏洞(XXE)

    转自腾讯安全应急响应中心 一.XML基础知识 XML用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.XML文档结构包括XML声 ...