链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=4585


题目:

Problem Description

Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The master of Shaolin evaluates a young man mainly by his talent on understanding the Buddism scripture, but fighting skill is also taken into account.
When a young man passes all the tests and is declared a new monk of Shaolin, there will be a fight , as a part of the welcome party. Every monk has an unique id and a unique fighting grade, which are all integers. The new monk must fight with a old monk whose fighting grade is closest to his fighting grade. If there are two old monks satisfying that condition, the new monk will take the one whose fighting grade is less than his.
The master is the first monk in Shaolin, his id is 1,and his fighting grade is 1,000,000,000.He just lost the fighting records. But he still remembers who joined Shaolin earlier, who joined later. Please recover the fighting records for him.

Input

There are several test cases.
In each test case:
The first line is a integer n (0

Output

A fight can be described as two ids of the monks who make that fight. For each test case, output all fights by the ascending order of happening time. Each fight in a line. For each fight, print the new monk’s id first ,then the old monk’s id.

Sample Input

3
2 1
3 3
4 2
0

Sample Output

2 1
3 2
4 2


题意:

  初始有一个编号为1能力值为INF的人在队列中,给你新人的数目n和n个新人的编号和能力值,每来一个新的人,他就会在比他先来的所有人中挑一个和他能力值相差最少的人进行比试,然后输出比试的名单。

  比较奇怪的一点是题目里明明说了the new monk will take the one whose fighting grade is less than his,刚开始怎么做都做不对,搜了一下题解结果却是找一个绝对值相差最少的,想不通。


思路:

  每新来一个人,就将他插入到树中,然后找他的前驱和后继,取绝对值小的那一个。


实现:

#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <iomanip>
#include <functional>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>#define read read()
#define edl putchar('\n')
#define clr(a, b) memset(a,b,sizeof a)using namespace std;
const int INF = 0x3f3f3f3f;
struct node {int fix, v, size;node *ch[2];node(int id, int v) : fix(rand()), v(v), size(1) {ch[0] = ch[1] = NULL;}void maintain() {size = 1;if (ch[0] != NULL) size += ch[0]->size;if (ch[1] != NULL) size += ch[1]->size;}int compare(int x) {if (x == v) return -1;return x < v ? 0 : 1;}
} *root;void rotate(node *&t, int d) {if (t == NULL) return;node *tmp = t->ch[d ^ 1];t->ch[d ^ 1] = tmp->ch[d];tmp->ch[d] = t;t->maintain();tmp->maintain();t = tmp;
}void insert(node *&t, int id, int v) {if (t == NULL) t = new node(id, v);else {int d = t->compare(v);insert(t->ch[d], id, v);if (t->ch[d]->fix > t->fix) rotate(t, d ^ 1);else t->maintain();}
}node* PreSuc(node *t, int x, int d) {// d = 0 : 前驱 , d = 1 : 后驱node * pre = NULL;while(t != NULL && t->v != x) {int k = t->compare(x);if(k == (d^1)) pre = t;t = t->ch[k];}t = t->ch[d];if(t == NULL) return pre;else {while(t->ch[d^1] != NULL) {t = t->ch[d^1];}return t;}
}int findrank(node *t, int x) {if(t == NULL) return -1;int r;if (t->ch[0] == NULL) r = 1;else r = t->ch[0]->size + 1;if (x == t->v) return r;if (x < t->v) return findrank(t->ch[0], x);return r + findrank(t->ch[1], x);
}void clear(node *&t) {if(t == NULL) return;clear(t->ch[0]);clear(t->ch[1]);delete t;t = NULL;
}void Print(node *t) {if (t == NULL) return;Print(t->ch[0]);cout << t->v << ' ';Print(t->ch[1]);
}int calc(node *t0, node *t1, int v) {int a[2];if(t0 == NULL) a[0] = INF+7;else a[0] = t0->v;if(t1 == NULL) a[1] = INF+7;else a[1] = t1->v;return abs(a[0] - v) > abs(a[1] - v) ? 1 : 0;
}int main() {
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
#endifreturn 0;
}

Shaolin - HDU 4585 - 树堆相关推荐

  1. Shaolin HDU - 4585(map模板题)

    题意: 少林寺有n+1个和尚,他们都有一个独有的编号和战斗力值,当一个年轻人通过所有考试并被宣布为少林的新僧人时,将会有一场战斗,作为欢迎的一部分.新和尚必须与一位战斗等级最接近他的战斗等级的老和尚战 ...

  2. Shaolin(map+iterator) HDU - 4585

    Shaolin(map+iterator) HDU - 4585 少林寺以武僧而闻名.每年都有很多年轻人去少林寺当和尚.少林的主人评估一个年轻人主要通过他的人才了解佛教经文,但武功也考虑在内. 当一个 ...

  3. SDUT 2127 树-堆结构练习——合并果子之哈夫曼树(优先队列)

    树-堆结构练习--合并果子之哈夫曼树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description ...

  4. 树-堆结构练习——合并果子之哈夫曼树

    树-堆结构练习--合并果子之哈夫曼树 Description 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆 ...

  5. 树-堆结构练习——合并果子之哈夫曼树(是最优二叉树题目的缩影)

    树-堆结构练习--合并果子之哈夫曼树 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 在一个果园里,多多已经将所有的果子 ...

  6. 树堆(Treap)图文详解与实现

    1.Treap的定义 树堆(Treap)是二叉排序树(Binary Sort Tree)与堆(Heap)结合产生的一种拥有堆性质的二叉排序树. 但是这里要注意两点,第一点是Treap和二叉堆有一点不同 ...

  7. Treap树堆(bzoj 3224: Tyvj 1728 普通平衡树)

    Treap树堆:一种排序二叉树(中序遍历权值有序) 每个节点有两个关键字:key[]和rand[] 其中key[]满足二叉搜索树性质,rand[]满足堆性质(即Tree+Heap=Treap)即 如果 ...

  8. 终于从树堆里爬出来了——堆排序(基于二叉树)基本思想、步骤、复杂度及python代码,欢迎交流

    欢迎关注,敬请点赞! 树堆逃生记 一.动图演示 二.思路分析 1. 相关概念 2. 基本思想 3. 步骤 [步骤一] 构造初始堆 [步骤二] 将堆顶元素与末尾元素进行交换,使末尾元素最大. [步骤三] ...

  9. 树-堆结构练习——合并果子之哈夫曼树 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Descrip

    树-堆结构练习--合并果子之哈夫曼树 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Descrip ...

  10. 树-堆结构练习——合并果子之哈夫曼树oj

    树-堆结构练习--合并果子之哈夫曼树 Time Limit: 1000MS Memory Limit: 65536KB Problem Description 在一个果园里,多多已经将所有的果子打了下 ...

最新文章

  1. 如果机器能帮我们学习,那么有多少东西能够被遗忘?
  2. 全排列(递归与非递归实现)
  3. c语言 goto 跳出循环,goto语句可以跳出循环.ppt
  4. java小编程----str中没有内容
  5. leetcode 282. Expression Add Operators | 282. 给表达式添加运算符(中缀表达式求值)
  6. 欢乐纪中某B组赛【2019.1.18】
  7. Spring的properties属性配置文件和Spring常用注解
  8. Struts2项目中的JSP如何访问Session对象
  9. 华尔街日报:微软告别盖茨时代
  10. BB-UNet:带有包围框先验的U-Net
  11. php7新特性的理解和比较
  12. 解决苹果手机ios系统app store无法下载讯飞有声的问题
  13. 计算机管理 灰色,详解电脑任务管理器变成灰色不可用的解决方法
  14. 含echarts图表の网页打印
  15. vivo S16/S15/S12/S10 PRO卡刷线刷系统升级降级推荐成功解决屏幕锁不记得开机锁成功刷好的有效方案
  16. javascript 实现购物车多项物品累计求总价案例 ,价格保留两位小数
  17. 前端js html转换成pdf可下载打印;前端js可批量生成条形码;前端js可批量生成二维码,生成letter标签
  18. 醉后不知天在水,满船清梦压星河。—第三十六天
  19. RichView 文档 段落 项目 ITEM
  20. python写酒店管理系统报告_酒店管理系统python

热门文章

  1. 天创速盈电商:拼多多用户群体分析
  2. c++哈利波特游戏(流行的版本的破解版)
  3. 智能DNS - 免费智能DNS解析服务-迄今为止最好用的智能DNS
  4. DNA测序技术发展史:一代、二代、三代测序技术简要原理及比较
  5. Android网络请求三篇
  6. P2627 [USACO11OPEN]Mowing the Lawn G(单调队列优化dp)
  7. win8计算机禁止休眠,win8系统设置禁止电脑休眠的教程方法
  8. java写关于温度的算法_摄氏温度和华氏温度的转换之java算法
  9. matlab中abs是什么函数,abs是什么函数(excel表格abs公式)
  10. 华山行-西安生活的小记录