hdu 4585 Shaolin

Shaolin

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

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 <n <=100,000),meaning the number of monks who joined Shaolin after the master did.(The master is not included).Then n lines follow. Each line has two integer k and g, meaning a monk’s id and his fighting grade.( 0<= k ,g<=5,000,000)
The monks are listed by ascending order of jointing time.In other words, monks who joined Shaolin earlier come first.
The input ends with 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

Source

2013ACM-ICPC杭州赛区全国邀请赛

Submit

STL map代码

#include <bits/stdc++.h>

using namespace std;
map<int, int> mp; // it->first是等级,it->second是id

int main()
{
    int n;
    while (~scanf("%d", &n) && n)
    {
        mp.clear();
        mp[1000000000] = 1; //方丈1,等级1000000000
        while (n--)
        {
            int id, g;
            scanf("%d %d", &id, &g); //新和尚id,等级是g
            mp[g] = id;              //新和尚进队
            int ans;
            map<int, int>::iterator it = mp.find(g); //找到排好序的位置
            if (it == mp.begin())
                ans = (++it)->second;
            else
            {
                map<int, int>::iterator it2 = it;
                it2--;
                it++; //等级接近的前后两个和尚
                if (g - it2->first <= it->first - g)
                    ans = it2->second;
                else
                    ans = it->second;
            }
            printf("%d %d\n", id, ans);
        }
    }
    return 0;
}

Treap(名次树)代码

#include <bits/stdc++.h>

using namespace std;
int id[5000000 + 5];

struct Node
{
    int size;     //以这个结点为根的子树的结点总数量,用于名次树
    int rank;     //优先级
    int key;      //键值
    Node *son[2]; // son[0]左儿子,son[1]右儿子

    bool operator<(const Node &a) const
    {
        return rank < a.rank;
    }

    int cmp(int x) const
    {
        if (x == key)
            return -1;
        return x < key ? 0 : 1;
    }

    void update() //更新size
    {
        size = 1;
        if (son[0] != NULL)
            size += son[0]->size;
        if (son[1] != NULL)
            size += son[1]->size;
    }
};

void rotate(Node *&o, int d) // d = 0,左旋,d = 1,右旋
{
    Node *k = o->son[d ^ 1]; // d^1 与 1-d 等价 ,但是更快
    o->son[d ^ 1] = k->son[d];
    k->son[d] = o;
    o->update();
    k->update();
    o = k;
}

void insert(Node *&o, int x) //把x插入到树中
{
    if (o == NULL)
    {
        o = new Node();
        o->son[0] = o->son[1] = NULL;
        o->rank = rand();
        o->key = x;
        o->size = 1;
    }
    else
    {
        int d = o->cmp(x);
        insert(o->son[d], x);
        o->update();
        if (o < o->son[d])
            rotate(o, d ^ 1);
    }
}

int kth(Node *o, int k) //返回第k大的数
{

    if (o == NULL || k <= 0 || k > o->size)
        return -1;
    int s = o->son[1] == NULL ? 0 : o->son[1]->size;
    if (k == s + 1)
        return o->key;
    else if (k <= s)
        return kth(o->son[1], k);
    else
        return kth(o->son[0], k - s - 1);
}

int find(Node *o, int k) //返回元素k的名次
{
    if (o == NULL)
        return -1;
    int d = o->cmp(k);
    if (d == -1)
        return o->son[1] == NULL ? 1 : o->son[1]->size + 1;
    else if (d == 1)
        return find(o->son[d], k);
    else
    {
        int temp = find(o->son[d], k);
        if (temp == -1)
            return -1;
        else
            return o->son[1] == NULL ? temp + 1 : temp + 1 + o->son[1]->size;
    }
}

int main()
{
    int n;
    while (~scanf("%d", &n) && n)
    {
        srand(time(NULL));
        int k, g;
        scanf("%d %d", &k, &g);
        Node *root = new Node();
        root->son[0] = root->son[1] = NULL;
        root->rank = rand();
        root->key = g;
        root->size = 1;
        id[g] = k;
        printf("%d %d\n", k, 1);
        for (int i = 2; i <= n; ++i)
        {
            scanf(" %d %d", &k, &g);
            id[g] = k;
            insert(root, g);
            int t = find(root, g); //返回新和尚的名次
            int ans1, ans2, ans;
            ans1 = kth(root, t - 1); //前一名老和尚
            ans2 = kth(root, t + 1); //后一名老和尚
            if (ans1 != -1 && ans2 != -1)
                ans = ans1 - g >= g - ans2 ? ans2 : ans1;
            else if (ans1 == -1)
                ans = ans2;
            else
                ans = ans1;
            printf("%d %d\n", k, id[ans]);
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追寻远方的人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值