HDOJ 1512 几乎模板的左偏树
题目大意:有n个猴子。每个猴子有一个力量值,力量值越大表示这个猴子打架越厉害。如果2个猴子不认识,他们就会找他们认识的猴子中力量最大的出来单挑,单挑不论输赢,单挑的2个猴子力量值减半,这2拨猴子就都认识了,不打不相识嘛。现在给m组询问,如果2只猴子相互认识,输出-1,否则他们各自找自己认识的最牛叉的猴子单挑,求挑完后这拨猴子力量最大值。
左偏大根加并查
代码都懒的贴了...
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <utility>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;
const int maxn = ;
struct node {
int l,r,dis,val,par;
} heap[maxn];
int N, M;
int find ( int &x ) {
return heap[x].par == x ? x : heap[x].par = find ( heap[x].par );
}
int merge(int rt1,int rt2)
{
if (rt1==) return rt2;
if (rt2==) return rt1;
if ( heap[rt2].val>heap[rt1].val )swap(rt1,rt2);
heap[rt1].r = merge(heap[rt1].r,rt2);
heap[heap[rt1].r].par = rt1;
if ( heap[ heap[rt1].l ].dis < heap[ heap[rt2].r ].dis )
swap ( heap[rt1].l, heap[rt1].r );
else heap[rt1].dis = heap[ heap[rt1].r ].dis + ;
return rt1;
}
int push ( int x, int y ) {
return merge ( x, y );
}
int pop ( int &x ) {
int l = heap[x].l;
int r = heap[x].r;
heap[l].par = l;
heap[r].par = r;
heap[x].l = heap[x].r = heap[x].dis = ;
return merge ( l, r );
}
bool scan_d(int &num) {
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<''||in>'')) in=getchar();
if(in=='-'){ IsN=true;num=;}
else num=in-'';
while(in=getchar(),in>=''&&in<=''){
num*=,num+=in-'';
}
if(IsN) num=-num;
return true;
}
int main() {
while ( scan_d ( N ) ) {
for ( int i = ; i <= N; ++ i ) {
scan_d ( heap[i].val );
heap[i].l = heap[i].r = heap[i].dis = ;
heap[i].par = i;
}
scan_d ( M );
int a, b, x, y;
while ( M -- ) {
scan_d (a); scan_d (b);
x = find ( a );
y = find ( b );
if ( x == y ) {
puts ( "-1" );
} else {
heap[x].val /= ;
int px = push ( pop ( x ), x );
heap[y].val /= ;
int py = push ( pop ( y ), y ); printf ( "%d\n", heap[ merge ( px, py ) ].val );
}
}
}
return ;
}
HDOJ 1512 几乎模板的左偏树的更多相关文章
- 洛谷 P3377 【模板】左偏树(可并堆)
洛谷 P3377 [模板]左偏树(可并堆) 题目描述 如题,一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 x y 将第x个数和第y个数所在的小根堆合并(若第x或 ...
- 模板 可并堆【洛谷P3377】 【模板】左偏树(可并堆)
P3377 [模板]左偏树(可并堆) 如题,一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 x y 将第x个数和第y个数所在的小根堆合并(若第x或第y个数已经被删 ...
- 洛谷P3377 【模板】左偏树(可并堆) 题解
作者:zifeiy 标签:左偏树 这篇随笔需要你在之前掌握 堆 和 二叉树 的相关知识点. 堆支持在 \(O(\log n)\) 的时间内进行插入元素.查询最值和删除最值的操作.在这里,如果最值是最小 ...
- HDU 1512 Monkey King(左偏树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1512 题意: 有n只猴子,每只猴子一开始有个力量值,并且互相不认识,现有每次有两只猴子要决斗,如果认识,就不打了 ...
- HDU 1512 Monkey King(左偏树+并查集)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1512 [题目大意] 现在有 一群互不认识的猴子,每个猴子有一个能力值,每次选择两个猴子,挑出他们所 ...
- [Luogu3377]【模板】左偏树(可并堆)
题面戳我 题目描述 如题,一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 x y 将第x个数和第y个数所在的小根堆合并(若第x或第y个数已经被删除或第x和第y个数 ...
- luogu【P3377】 【模板】左偏树
左偏树 顾名思义 向左偏的树 (原题入口) 它有啥子用呢??? 当然是进行堆的合并啦2333普通堆的合并其实是有点慢的(用优先队列的话 只能 一个pop 一个push 来操作 复杂度就是O(n log ...
- HDU 1512 Monkey King(左偏树)
Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each does thi ...
- [洛谷P3377]【模板】左偏树(可并堆)
题目大意:有$n$个数,$m$个操作: $1\;x\;y:$把第$x$个数和第$y$个数所在的小根堆合并 $2\;x:$输出第$x$个数所在的堆的最小值 题解:左偏树,保证每个的左儿子的距离大于右儿子 ...
随机推荐
- valgind使用错误——检测不同位目标程序
当64位的valgrind工具测试32位的程序时,会报如下错误: ==22235== Memcheck, a memory error detector ==22235== Copyright (C) ...
- Python chr() ord() unichr()
chr()函数用一个范围在range(256)内的(就是0-255)整数作参数,返回一个对应的字符. unichr()跟它一样,只不过返回的是Unicode字符,这个从Python 2.0才加入的un ...
- Jquery数组操作
jQuery的数组处理,便捷,功能齐全. 最近的项目中用到的比较多,深感实用,一步到位的封装了很多原生js数组不能企及的功能. 最近时间紧迫,今天抽了些时间回过头来看 jQuery中文文档 中对数组的 ...
- ecstore菜鸟电子面单对接摘要
1.token长度要150以上 2.redirect_uri不一致,保持一致吧先 3.返回的xml解析问题,php5.3里得到的不是数组,而是simplexml,还得再转化一下 4.后期考虑做成通用型 ...
- c语言实现二维数组排序,一个4*5的数组,要求每行都进行升序排列,并求出每行的平均值。
#include<stdio.h>#define N 5#define M 4void main(){ int x,i,j,k,t,a[M][N]; float sum=0.0; floa ...
- DELPHI高性能大容量SOCKET并发(九):稳定性问题解决
http://blog.csdn.net/sqldebug_fan/article/details/9043699
- sp_help 快速查看表结构、视图信息
sp_helptext: 是MS SQL Server的一个系统存储过程,可以通过它来查看存储过程或者视图.函数源码 示例:sp_helptext viewName (viewName 即要查询的存 ...
- WPF绑定数据源
using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.Comp ...
- python 中 五种字典(dict)的遍历方法,实验法比较性能。
1 .背景: 想知道5种遍历方法,并且知道从性能角度考虑,使用哪种. 2.结论: 使用这种方式: for key,val in AutoDict.iteritems(): temp = "% ...
- Node.js初探之hello world
昨天公司内部培训,主讲人王老板对Node.js评价很高,连用几个“变态”来形容,恰好今天周末,有时间来认识下Node.js,对一门新语言最好的认识,是让其输出“hello world”,今天我就利用N ...