题目大意
解题思路
(1): 武力值唯一,使用武力值作为key
建字典map<int, int> f2id
。
(2): 字典中的元素时按key
升序排列的,因此直接调用ite = f2id.lower_bound(f)
查找第一个key>=f
的元素
(3): ite
只能进行ite++
或者ite--
。
(4): 若没有找到武力值比f
大的,则有ite == f2id.end()
代码
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
map<int, int> f2id;
int main()
{
int n;
cin >> n;
f2id[1000000000] = 1;
int id, f;
map<int, int>::iterator ite, itep, itel;
while(n--)
{
cin >> id >> f;
ite = f2id.lower_bound(f);
if(ite == f2id.begin())
cout << id << " " << ite->second << endl;
else if(ite == f2id.end())
{
ite--;
cout << id << " " << ite->second << endl;
}
else
{
int pid, pf;
int lid, lf;
ite--;
pid = ite->second;
pf = ite->first;
ite++;
lid = ite->second;
lf = ite->first;
if(lf-f == f-pf)
cout << id << " " << pid << endl;
else if(lf-f> f-pf)
cout << id << " " << pid << endl;
else
cout << id << " " << lid << endl;
}
f2id[f] = id;
}
return 0;
}