HDU_4585_Shaolin

HDU_4585_Shaolin
题意:少林寺招新,起初只有一个id为1武力值为1000000000的老和尚,现在要招一些新和尚,每个和尚入寺考核要和已经入寺的和尚比武且只和武力值与他相差最小的比武,当有两个和尚与他武力值差值相同时(比如大1和小1)便在这些差值相同的和尚里只和比他小的比武。
思路:用STL中的映射,用武力值作为键,然后找已入寺的和尚比武时用lower_bound函数查找。


#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <algorithm>
#include <functional>

#define debug(x) cout << "--------------> " << x << endl

using namespace std;

const double PI = acos(-1.0);
const double eps = 1e-10;
const long long INF = 0x7fffffff;
const long long MOD = 1000000007;

int main()
{
    int n;
    while(scanf("%d", &n), n)
    {
        map<int, int> m;
        map<int, int>::iterator it;
        int a, b;
        m[1000000000] = 1;
        for(int i = 0; i < n; ++i)
        {
            scanf("%d%d", &a, &b);
            printf("%d ", a);
            it = m.lower_bound(b);
            if(it == m.begin() || it == m.end())
            {
                printf("%d\n", it->second);
            }
            else
            {
                //map按键值默认排序
                int a1 = it->first, b1 = it->second;
                it--;
                int a2 = it->first, b2 = it->second;
                if((b-a2) <= (a1-b))
                {
                    printf("%d\n", b2);
                }
                else
                    printf("%d\n", b1);

            }
            m[b] = a;
        }
    }
     return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值