【HDOJ】1512 Monkey King
左偏树+并查集。左偏树就是可合并二叉堆。
/* 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的更多相关文章
- 【HDOJ】1069 Monkey and Banana
DP问题,我是按照边排序的,排序既要考虑x也要考虑y,同时在每个面中,长宽也要有序.还有注意状态转移,当前高度并不是之前的最大block叠加的高度,而是可叠加最大高度+当前block高度或者是当前bl ...
- 【转】Android Monkey 命令行可用的全部选项
常规 事件 约束限制 调试 原文参见:http://www.douban.com/note/257030384/ 常规 –help 列出简单的用法. -v 命令行的每一个 -v 将增加反馈信息的级别. ...
- HDU 1512 Monkey King(左偏树+并查集)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1512 [题目大意] 现在有 一群互不认识的猴子,每个猴子有一个能力值,每次选择两个猴子,挑出他们所 ...
- HDU 1512 Monkey King ——左偏树
[题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...
- 数据结构(左偏树):HDU 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 【模拟】Codeforces 710A King Moves
题目链接: http://codeforces.com/problemset/problem/710/A 题目大意: 国际象棋标准8X8棋盘,国王能往周围8个方向走.输入国王的位置,输出当前国王能往几 ...
- 【带权并查集】【HDOJ】
http://acm.hdu.edu.cn/showproblem.php?pid=3047 Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) ...
- 【bzoj1087】互不侵犯King 状态压缩dp
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=1087 [题解] 用f[i][j][k]表示前i行放了j个棋子且第i行的状态为k的方案数. ...
- 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
随机推荐
- JBPM WEB CONSOLE安装实录
http://www.blogjava.net/paulwong/archive/2009/03/13/259551.html JBPM WEB CONSOLE是一个B/S端的,能管理JBPM的流程和 ...
- Jquery Ajax 调用 WebService
原文:http://www.cnblogs.com/andiki/archive/2010/05/17/1737254.html jquery ajax调用webservice(C#)要注意的几个事项 ...
- Openfire:安装指南
本文的英文原文来自 http://www.igniterealtime.org/builds/openfire/docs/latest/documentation/install-guide.html ...
- Java线程间通信-回调的实现方式
Java线程间通信-回调的实现方式 Java线程间通信是非常复杂的问题的.线程间通信问题本质上是如何将与线程相关的变量或者对象传递给别的线程,从而实现交互. 比如举一个简单例子,有一个多线程的 ...
- Ztree使用
基础: <link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type ...
- 查内网虚拟机映射的公网IP
1.访问ip138.com 2.curl ifconfig.me
- 网卡添加VLAN TAG
#modprobe 8021q 用命令 lsmod | grep 8021q 来检查 以配置网卡eth0为例,添加vlan号:1002 ================================ ...
- Library string Type
The string type supports variable-length character strings.The library takes cares of managing memor ...
- php安装过程中遇到的需要安装的问题
http://www.cnblogs.com/kristain/articles/3809243.html 借鉴php安装错误 2013-01-04 19:16:49 分类: 系统运维 环境: ...
- django 学习点滴
django连接数据库要安装第三方包,比如mysql的就是 python-mysqldb, 用apt-cache search python-mysql 搜索一下. django的project可以放 ...