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

 /* 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. 【C#】获取本地Cookie的问题

    using System; using System.Net; using System.IO; using System.Text; // // TODO: 在此处添加代码以启动应用程序 // st ...

  2. 在office 2010中插入Mathtype出现ctrl+v不能复制的问题

    加载项中去掉command for那个即可 详细网址:http://heblue.blog.163.com/blog/static/96325568201375102628405/

  3. window redis 安装配置

    1 下载 https://github.com/MSOpenTech/redis/releases 当前最新版本为 redis-2.8.21   下载的为zip包,下载连接为:https://gith ...

  4. 【制作镜像】BCEC制作镜像

    如要制作的新镜像已存在标准版本镜像,即linux发行版本相同(此处指CentOS6.5 64位),可利用BCEC制作. 在BCEC创建centos6.5系统的可联外网的虚机,ssh到此虚机,用yum方 ...

  5. rpm命令详解

    http://www.rpm.org/max-rpm/s1-rpm-install-additional-options.html#S2-RPM-INSTALL-REPLACEFILES-OPTION ...

  6. git push后修改错误的commit message

    Easiest solution (but please read this whole answer before doing this): git rebase -i <hash-of-co ...

  7. jQuery 滚动动画简单版

    动画的思路很简单,点击页面上一个元素,页面滚动到指定位置.下面介绍一下我3个小时百度的研究成果: 首先是html部分: <html> <body> <a>顶部< ...

  8. (WinForm)FormBorderStyle属性

    此属性就是获取或设置窗体的边框样式,默认值为 FormBorderStyle.Sizable.共7个值. 属性 意义 None 无边框 FixedSingle 固定的单行边框 Fixed3D 固定的三 ...

  9. 入门2:PHP相关的名词解释

    /**宝宝我英语不好,后面注释拼音 请见谅**/ 1.Linux 开源的操作系统,在服务器端用户数量非常大,很多服务器都是使用Linux系统运行的. 相对windows系统来说具有非常完善的用户权限系 ...

  10. xml之phpdom操作

    php xml编程XML解析技术介绍 1.php与DOM 2.PHP与XPath 3.SimpleXML DOM(document object model)文档对象模型 把一个文件看做一个对象模型, ...