hdu 1434 幸福列车 (Leftist Tree)

本文详细介绍了如何使用左偏堆解决HDU 1434题目,提供了从题解到代码实现的全过程,包括复杂度分析和优化策略,特别强调了左偏堆相较于普通堆的优越性及其在解决此类问题时的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem - 1434

  网上题解是普通的堆合并,都是用优先队列直接做的。可是正解的堆合并应该是用左偏堆或者斐波那契堆的吧,不然O(X * N ^ 2)的复杂度应该是过不了的。斐波那契堆的实现相对麻烦,所以我用了左偏堆完成这题,最坏复杂度O(X * N log N)。

  这题就是一个模拟,没什么可以解释的。1y~

代码如下:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 template<class T>
10 struct Node {
11     int d;
12     T dt;
13     Node *l, *r;
14     Node() { l = r = NULL;}
15     Node(T dt) : dt(dt), d(0) { l = r = NULL;}
16 } ;
17 
18 template<class T>
19 Node<T> *merge(Node<T> *a, Node<T> *b) {
20     if (!a) return b;
21     if (!b) return a;
22     if (a->dt < b->dt) return merge(b, a);
23     a->r = merge(a->r, b);
24     a->d = a->r ? a->r->d + 1 : 0;
25     return a;
26 }
27 
28 template<class T>
29 Node<T> *popTop(Node<T> *x) { Node<T> *ret = merge(x->l, x->r); delete x; return ret;}
30 
31 template<class T>
32 struct Leftist {
33     Node<T> *rt;
34     void clear() { rt = NULL;}
35     T top() { return rt->dt;}
36     void push(T dt) { rt = merge(rt, new Node<T>(dt));}
37     void pop() { rt = popTop(rt);}
38 } ;
39 
40 const int N = 11111;
41 typedef pair<int, string> PIS;
42 Leftist<PIS> pq[N];
43 char buf[111];
44 
45 int main() {
46     int n, m;
47     while (~scanf("%d%d", &n, &m)) {
48         int k, x, y;
49         for (int i = 1; i <= n; i++) {
50             pq[i].clear();
51             scanf("%d", &k);
52             for (int j = 0; j < k; j++) {
53                 scanf("%s%d", buf, &x);
54                 pq[i].push(PIS(-x, buf));
55             }
56         }
57         for (int i = 0; i < m; i++) {
58             scanf("%s%d", buf, &x);
59             if (!strcmp(buf, "GETOUT")) {
60                 puts(pq[x].top().second.c_str());
61                 pq[x].pop();
62             }
63             if (!strcmp(buf, "JOIN")) {
64                 scanf("%d", &y);
65                 pq[x].rt = merge(pq[x].rt, pq[y].rt);
66                 pq[y].rt = NULL;
67             }
68             if (!strcmp(buf, "GETON")) {
69                 scanf("%s%d", buf, &y);
70                 pq[x].push(PIS(-y, buf));
71             }
72         }
73     }
74     return 0;
75 }
View Code

 

——written by Lyon

转载于:https://www.cnblogs.com/LyonLys/p/hdu_1434_Lyon.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值