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$个数所在的堆的最小值 题解:左偏树,保证每个的左儿子的距离大于右儿子 ...
随机推荐
- Uncaught TypeError: _react2.default.findDOMNode is not a function
react 查找某节点时报错 Uncaught TypeError: _react2.default.findDOMNode is not a function 代码: import React, { ...
- Windows 10 error code 0x80072efd
访问windows 10 store出现如下图的错误: 如果你是宽带连接,删掉现在的连接,新建一个同样的宽带连接就OK了. 哎,这个问题导致我半个月没上去Windows Store,我还真以为微软有问 ...
- 操作配置文件Properties
// */ // ]]> 操作配置文件Properties Table of Contents 1 定义 2 读取配置值 3 修改和保存配置 4 注意 1 定义 csharp中在Settin ...
- Google Tensorflow 源码编译(一):Protobuf<v3.0.0-alpha-3>
这几天终于把tensorflow安装上了,中间遇到过不少的问题,这里记录下来.供大家想源码安装的参考. 安装环境:POWER8处理器,Docker容器Ubuntu14.04镜像. Build Prot ...
- Intellij Idea系列之Tomcat环境的搭建(三)
Intellij Idea系列之Tomcat环境的搭建(三) 一. 编写背景 Intellij Idea在刚上手的时候很多人吐槽,"god, 这么难用的IDE有谁用呀?",的确,I ...
- reorderList使用
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" /> ...
- Mathematica 计算矩阵的伴随矩阵
AdjointMatrix[M_] := Module[{Ma, B, n, i, j}, Ma = Minors[M]; B = Ma; n = Dimensions[M][[1] ...
- php数据库访问
从$res获取行数据的时候,处理mysql_fetch_row($res),还有三个方法,分别是 mysql_fetch_row($res); 返回一个所以的数组,速度较快. mysql_fetch_ ...
- PASCAL==CALLBACK==WINAPI==__stdcall
VC里面:PASCAL==CALLBACK==WINAPI==__stdcall _stdcall是Pascal程序的缺省调用方式,通常用于Win32 Api中,函数采用从右到左的压 ...
- el 中requestScope和param
${scope.attribute},其中scope指pageSocpe.requestScope.sessionScope.applicationScope,attribute指的就是你在某个sco ...