hdu 1434 幸福列车 (Leftist Tree)
网上题解是普通的堆合并,都是用优先队列直接做的。可是正解的堆合并应该是用左偏堆或者斐波那契堆的吧,不然O(X * N ^ 2)的复杂度应该是过不了的。斐波那契堆的实现相对麻烦,所以我用了左偏堆完成这题,最坏复杂度O(X * N log N)。
这题就是一个模拟,没什么可以解释的。1y~
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string> using namespace std; template<class T>
struct Node {
int d;
T dt;
Node *l, *r;
Node() { l = r = NULL;}
Node(T dt) : dt(dt), d() { l = r = NULL;}
} ; template<class T>
Node<T> *merge(Node<T> *a, Node<T> *b) {
if (!a) return b;
if (!b) return a;
if (a->dt < b->dt) return merge(b, a);
a->r = merge(a->r, b);
a->d = a->r ? a->r->d + : ;
return a;
} template<class T>
Node<T> *popTop(Node<T> *x) { Node<T> *ret = merge(x->l, x->r); delete x; return ret;} template<class T>
struct Leftist {
Node<T> *rt;
void clear() { rt = NULL;}
T top() { return rt->dt;}
void push(T dt) { rt = merge(rt, new Node<T>(dt));}
void pop() { rt = popTop(rt);}
} ; const int N = ;
typedef pair<int, string> PIS;
Leftist<PIS> pq[N];
char buf[]; int main() {
int n, m;
while (~scanf("%d%d", &n, &m)) {
int k, x, y;
for (int i = ; i <= n; i++) {
pq[i].clear();
scanf("%d", &k);
for (int j = ; j < k; j++) {
scanf("%s%d", buf, &x);
pq[i].push(PIS(-x, buf));
}
}
for (int i = ; i < m; i++) {
scanf("%s%d", buf, &x);
if (!strcmp(buf, "GETOUT")) {
puts(pq[x].top().second.c_str());
pq[x].pop();
}
if (!strcmp(buf, "JOIN")) {
scanf("%d", &y);
pq[x].rt = merge(pq[x].rt, pq[y].rt);
pq[y].rt = NULL;
}
if (!strcmp(buf, "GETON")) {
scanf("%s%d", buf, &y);
pq[x].push(PIS(-y, buf));
}
}
}
return ;
}
——written by Lyon
hdu 1434 幸福列车 (Leftist Tree)的更多相关文章
- hdu 1434 幸福列车
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1434 幸福列车 Description 一批幸福的列车即将从杭州驶向幸福的终点站——温州,身为总列车长 ...
- HDU 1434 幸福列车(优先队列)
优先队列的应用 #include<iostream> #include<cstdio> #include<cstring> #include<queue> ...
- hdu 4912 Paths on the tree(树链拆分+贪婪)
题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道.要求尽量选出多的通道,而且两两通道不想交. 解题思路:用树链剖分求LCA,然后依据通道两端节点的LC ...
- 『左偏树 Leftist Tree』
新增一道例题 左偏树 Leftist Tree 这是一个由堆(优先队列)推广而来的神奇数据结构,我们先来了解一下它. 简单的来说,左偏树可以实现一般堆的所有功能,如查询最值,删除堆顶元素,加入新元素等 ...
- (hdu)5423 Rikka with Tree (dfs)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5423 Problem Description As we know, Rikka is p ...
- 【hdu 6161】Big binary tree(二叉树、dp)
多校9 1001 hdu 6161 Big binary tree 题意 有一个完全二叉树.编号i的点值是i,操作1是修改一个点的值为x,操作2是查询经过点u的所有路径的路径和最大值.10^5个点,1 ...
- HDU 6191 Query on A Tree(可持久化Trie+DFS序)
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
- hdu 5534 (完全背包) Partial Tree
题目:这里 题意: 感觉并不能表达清楚题意,所以 Problem Description In mathematics, and more specifically in graph theory, ...
- 【HDU 4408】Minimum Spanning Tree(最小生成树计数)
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
随机推荐
- 洛谷P1368 均分纸牌(加强版) [2017年6月计划 数论14]
P1368 均分纸牌(加强版) 题目描述 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,纸牌总数必为 N 的倍数.可以在任一堆上取1张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取 ...
- QT生成GUID
#include <QCoreApplication> #include <QUuid> #include <QDebug> int main(int argc, ...
- HDU 2686 双进程DP
//第一次遇到这种DP,看大牛的博客都是用最大流求解的...dp[k][i][j] 表示走k步,第一条路线横向走了i步,第二条路线横向走了j步,所获得的最大值.. //转移方程也很好想 #includ ...
- 对C语言内存对齐的初步了解
在解释内存对齐的作用前,先来看下内存对齐的规则: 1. 对于结构的各个成员,第一个成员位于偏移为0的位置,以后每个数据成员的偏移量必须是min(#pragma pack()指定的数,这个数据成员的自身 ...
- java如何访问memcache
1 Memcache是什么 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的 ...
- 微信小程序开发之图片等比例缩放 获取屏幕尺寸图片尺寸 自适应
wxml: <image style="width: {{imagewidth}}px; height: {{imageheight}}px;" src="{{i ...
- Python datetime模块的其他方法
- 【JZOJ4746】【NOIP2016提高A组模拟9.3】树塔狂想曲
题目描述 相信大家都在长训班学过树塔问题,题目很简单求最大化一个三角形数塔从上往下走的路径和.走的规则是:(i,j)号点只能走向(i+1,j)或者(i+1,j+1).如下图是一个数塔,映射到该数塔上行 ...
- Spring → 01:概述
一.了解Spring发展 Spring是一个开源框架,Spring是于2003年兴起的一个轻量级的Java开发框架,由Rod Johnson在其著作Expert One-On-One J2EE Dev ...
- jquery find 推荐
https://codeplayer.vip/p/j7soa 这篇写的还是不错的,备用. // 返回jQuery对象所有匹配元素的标识信息数组 // 每个元素形如:tagName或tagName#id ...