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

 /* 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. ActiveMQ系列(1) - 使用入门

    没网的日子真的不好过啊 1.背景:                   对于常见业务中,数据并发是一个很头疼的问题,很多时候,会出现资源共享导致线程阻塞的问题,这时候问题就来了,,,老板也尾随来了,来 ...

  2. 20160331javaweb 之JSP page 指令

  3. MVC中使用jquery的浏览器缓存问题

    jquery在浏览器ajax调用的时候,对缓存提供了很好的支持,POST方式不能被缓存,使用POST的原因,明确了数据不能被缓存,或者避免JSON攻击(JSON返回数据的时候可以被入侵) jquery ...

  4. sqlserver中的锁

    NOLOCK(不加锁) 此选项被选中时,SQL Server 在读取或修改数据时不加任何锁. 在这种情况下,用户有可能读取到未完成事务(Uncommited Transaction)或回滚(Roll ...

  5. Web前端/后端

       Web前端:         1)精通HTML,能够书写语义合理,结构清晰,易维护的HTML结构.         2)精通CSS,能够还原视觉设计,并兼容业界承认的主流浏览器.         ...

  6. JDBC——架构层、驱动

    JDBC(java Datebase Connector) jdbc驱动程序 四种类型: jdbc-odbc桥接驱动程序 Native-API JDBC-Net Native-Protocol (常见 ...

  7. Runtime 在IOS中的详细使用

    因为之前写东西的时候,都在未知笔记中,所有大家没有看到过我的文章,今天就开始使用博客园来进行和大家沟通.好了,废话不那么多了,转入正题.下面我把runtime 给大家介绍一下. ### 一.runti ...

  8. C++里容易忽视却不能忽视的

    1 define 只是简单地文本替换. 2 每个机器的字长不同. 3 每个类型在不同的机器上,所占用的内存空间不同. 4 每个机器内部的字节大小端不同. 5 并不是所有的编译器或机器都支持最新的C++ ...

  9. 使用Chrome DevTools的Timeline分析页面性能

    随着webpage可以承载的表现形式更加多样化,通过webpage来实现更多交互功能,构建web应用程序已经成为很多产品的首要选择.这种方式拥有非常明显的优势:跨平台.开发便捷.便于部署和维护等等,但 ...

  10. php中getimagesize函数的用法

    php获取图片信息getimagesize,php自带函数.获取图片的类型,尺寸的方法有许多,该函数仅是方法之一. getimagesize() 函数将测定任何 GIF,JPG,PNG,SWF,SWC ...