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 ...
随机推荐
- 2018-2-13-wpf-PreviewTextInput-在鼠标输入获得-_u0003
title author date CreateTime categories wpf PreviewTextInput 在鼠标输入获得 � lindexi 2018-2-13 17:23:3 +08 ...
- python 对位运算
- Liferay 7 module项目的依赖问题
build.gradle中的dependencies和bnd.bnd的Private-Package的关系是,build.gradle解决编译时候所需的所有依赖问题,但是这些依赖并不会被打包到buil ...
- 来自 Spring Cloud 官方的消息,Spring Cloud Alibaba 即将毕业
2019 年 7 月 24 日晚,Spring Cloud 官方发布公告: 仓库迁移是官方决定 Spring Cloud Alibaba 即将毕业 根据官方最新的发版规则,我们会把孵化器中的 Spri ...
- 移动端的touch事件(一)
如果我们允许用户在页面上用类似桌面浏览器鼠标手势的方式来控制WEB APP,这个页面上肯定是有很多可点击区域的,如果用户触摸到了那些可点击区域怎么办呢?? 诸如智能手机和平板电脑一类的移动设备通常会有 ...
- 【JZOJ4895】【NOIP2016提高A组集训第16场11.15】三部曲
=v= 因为外来的入侵,国王决定在某些城市加派士兵.所有城市初始士兵数量为0.当城市 被加派了k名士兵时.城市i的所有子城市需要被加派k+1名士兵.这些子城市的所有子城市需要被加派k+2名士兵.以此类 ...
- day40-Spring 02-事务的回顾
- thinkphp5.0 路由规则配置
开启路由‘url_route_on’ => true 首页路由'/' =>'home/index/index' 其它路由(1)'route' => 'home/index/route ...
- ArcMap影像纠偏
客户给了一张PNG图,如下图,需要提取其中北极航线. 策略是ArcMap中先配准PNG,使之与底图较好重合.再新建线图层,描出航线.这种彩色丰富的图,很难用栅格转矢量的方式,故应用描边法. 一.配准P ...
- IO流之字符流-1
Reader和Writer抽象类 Reader是定义java的流式字符输入流模式的抽象类 Writer是定义流式字符输出的抽象类 该类的方法都返回void值并在错条件下抛出IOException异常 ...