文章目录
Shaolin

题目大意
和尚战斗,第一个老和尚编号为 1 战斗力为 1e9 ,后面的和尚与其战斗,战斗值要与新人的战斗值差不多,然后新人就成了老和尚,如果两个老和尚战斗值相同,就输出编号小的值,最后输出新人的编号和与其战斗的和尚的编号。
直接
m
a
p
map
map 瞎几把搞。
直接把和尚的战斗力设成
k
e
y
key
key,
i
d
id
id 是保存的
d
a
t
a
data
data,二分一下,
m
a
p
map
map 容器中存在多个元素时,取
k
e
y
key
key 小的
d
a
t
a
data
data。
#include <bits/stdc++.h>
#define s second
#define f first
using namespace std;
const int maxn=1e9;
int n;
map<int,int>mp;
int main()
{
while(~scanf("%d",&n) && n)
{
mp.clear();
mp[maxn] = 1;
int id,k;
while(n--)
{
int ans;
scanf(" %d%d",&id,&k);
printf("%d ",id);
map<int,int>::iterator it1,it2;
it1 = mp.lower_bound(k); //返回一个迭代器,指向键值大于k的第一个元素
if(it1 == mp.begin()) ans= it1->s;
else
{
it2=it1;
if(it2->f - k >= k-(--it1)->f ) ans = it1->s;
else ans = it2->s;
}
printf("%d\n",ans);
mp[k] = id;
}
}
return 0;
}