Shaolin - HDU 4585 - 树堆

链接:

  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_JUDGE
    freopen("in.txt", "r", stdin);
#endif

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值