左偏树+并查集。左偏树就是可合并二叉堆。

 /* 1512 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct node_t {
int l, r, f, v, dis; node_t() {} node_t(int l_, int r_, int f_, int v_):
l(l_), r(r_), f(f_), v(v_) {} } node_t; const int maxn = 1e5+;
node_t H[maxn]; int find(int x) {
if (H[x].f == x)
return x;
return H[x].f = find(H[x].f);
} int merge(int a, int b) {
if (a == ) return b;
if (b == ) return a; if (H[a].v < H[b].v) swap(a, b);
H[a].r = merge(H[a].r, b);
H[H[a].r].f = a;
if (H[H[a].l].dis < H[H[a].r].dis) swap(H[a].l, H[a].r);
H[a].dis = H[H[a].r].dis + ; return a;
} int pop(int a) {
int l = H[a].l, r = H[a].r; H[l].f = l;
H[r].f = r; H[a].l = H[a].r = H[a].dis = ;
return merge(l, r);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int n, m;
int x, y;
int fx, fy;
int ans; H[].dis = -;
H[].l = H[].r = H[].f = H[].v = ;
while (scanf("%d", &n) != EOF) {
rep(i, , n+) {
scanf("%d", &H[i].v);
H[i].l = H[i].r = H[i].dis = ;
H[i].f = i;
} scanf("%d", &m);
while (m--) {
scanf("%d %d", &x, &y);
fx = find(x);
fy = find(y); if (fx == fy) {
puts("-1");
} else {
H[fx].v >>= ;
x = pop(fx);
x = merge(x, fx); H[fy].v >>= ;
y = pop(fy);
y = merge(y, fy); x = merge(x, y);
ans = H[x].v;
printf("%d\n", ans);
}
}
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

然后用优先级队列+并查集试了一下居然也过了,左偏树是764ms,而优先级队列是811ms。

 /* 1512 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = 1e5+;
int pre[maxn];
priority_queue<int, vi, less<int> > Q[maxn]; int find(int x) {
if (x == pre[x])
return x;
return pre[x] = find(pre[x]);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int n, m;
int x, y, fx, fy;
int ma, mb;
int ans, tmp; while (scanf("%d", &n)!=EOF) {
rep(i, , n+) {
scanf("%d", &tmp);
while (!Q[i].empty())
Q[i].pop();
Q[i].push(tmp);
pre[i] = i;
} scanf("%d", &m);
while (m--) {
scanf("%d %d", &x, &y);
fx = find(x);
fy = find(y); if (fx == fy) {
puts("-1");
} else {
ma = Q[fx].top();
Q[fx].pop();
Q[fx].push(ma>>); mb = Q[fy].top();
Q[fy].pop();
Q[fy].push(mb>>); if (SZ(Q[fx]) > SZ(Q[fy]))
swap(fx, fy); pre[fx] = fy;
while (!Q[fx].empty()) {
Q[fy].push(Q[fx].top());
Q[fx].pop();
} ans = Q[fy].top();
printf("%d\n", ans);
}
}
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【HDOJ】1512 Monkey King的更多相关文章

  1. 【HDOJ】1069 Monkey and Banana

    DP问题,我是按照边排序的,排序既要考虑x也要考虑y,同时在每个面中,长宽也要有序.还有注意状态转移,当前高度并不是之前的最大block叠加的高度,而是可叠加最大高度+当前block高度或者是当前bl ...

  2. 【转】Android Monkey 命令行可用的全部选项

    常规 事件 约束限制 调试 原文参见:http://www.douban.com/note/257030384/ 常规 –help 列出简单的用法. -v 命令行的每一个 -v 将增加反馈信息的级别. ...

  3. HDU 1512 Monkey King(左偏树+并查集)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1512 [题目大意] 现在有 一群互不认识的猴子,每个猴子有一个能力值,每次选择两个猴子,挑出他们所 ...

  4. HDU 1512 Monkey King ——左偏树

    [题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...

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

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

  6. 【模拟】Codeforces 710A King Moves

    题目链接: http://codeforces.com/problemset/problem/710/A 题目大意: 国际象棋标准8X8棋盘,国王能往周围8个方向走.输入国王的位置,输出当前国王能往几 ...

  7. 【带权并查集】【HDOJ】

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)   ...

  8. 【bzoj1087】互不侵犯King 状态压缩dp

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=1087 [题解] 用f[i][j][k]表示前i行放了j个棋子且第i行的状态为k的方案数. ...

  9. 1512 Monkey King

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

随机推荐

  1. SQL SERVER语句汇总

    1.查询数据库中所有用户表名:用户表总数. select name from dbo.sysobjects where OBJECTPROPERTY(id,N'IsUserTable')=1 sele ...

  2. Win7上安装oracle中可能遇到的错误

    安装oracle,总是出现一个警告两个错误错误,其描述是:OUI-18001:不支持操作系统’Windows Vista版本6.1’,找了好久,终于找到原因,因为oracle不支持Win7操作系统. ...

  3. js hashMap

    /** * MAP对象,实现MAP功能 * * 接口: * size() 获取MAP元素个数 * isEmpty() 判断MAP是否为空 * clear() 删除MAP所有元素 * put(key, ...

  4. js 无刷新分页代码

    /** * 分页事件处理 */function paging(){ $("#firstPage").click(function(){ //首页 var pageNo = getP ...

  5. HeadFirst设计模式

    oo基础 抽象 封装 多态 继承 oo原则 封装变化 多用组合,少用继承 针对接口编程,不针对实现编程(策略模式) 为交互对象之间的松耦合设计而努力(观察者模式) 对扩展开放,对修改关闭(装饰者模式) ...

  6. C#使用Json

    AJAX传递复杂数据如果自己进行格式定义的话会经历组装.解析的过程,因此AJAX中有一个事实上的数据传输标准JSon. Json将复杂对象序列化为一个字符串,在浏览器端再将字符串反序列化为JavaSc ...

  7. 页面mask css

    <html> <head> <style type="text/css"> .share_mask { position: fixed; top ...

  8. python 自动化之路 day 07 面向对象基础

    本节内容:   面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法.   面向对象编程 OOP编程是利用"类"和"对象" ...

  9. jquery-ui-datepicker定制化,汉化,因手机布局美观化源码修改

    感谢浏览,欢迎交流=.= 公司微信网页需要使用日历控件,想到jquery-mobile,但是css影响页面布局,放弃后使用jquery-ui-datepicker. 话不多说,进入正题: 1.jque ...

  10. OpenCV2学习笔记01:Linux下OpenCV开发环境的搭建

    个人已经厌倦了Windows下的开发方式,于是决定转到Linux平台上来,当然我也知道这个转变会很艰辛,但是我还是要坚持.所以,后面的所有开发我都会基于Linux和Qt,先从开发环境的搭建开始做起,当 ...