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;
}