042:热血格斗场
题面
描述
为了迎接08年的奥运会,让大家更加了解各种格斗运动,facer新开了一家热血格斗场。格斗场实行会员制,但是新来的会员不需要交入会费,而只要同一名老会员打一场表演赛,证明自己的实力。
我们假设格斗的实力可以用一个正整数表示,成为实力值。另外,每个人都有一个唯一的id,也是一个正整数。为了使得比赛更好看,每一个新队员都会选择与他实力最为接近的人比赛,即比赛双方的实力值之差的绝对值越小越好,如果有两个人的实力值与他差别相同,则他会选择比他弱的那个(显然,虐人必被虐好)。
不幸的是,Facer一不小心把比赛记录弄丢了,但是他还保留着会员的注册记录。现在请你帮facer恢复比赛纪录,按照时间顺序依次输出每场比赛双方的id。
输入
第一行一个数n(0 < n <=100000),表示格斗场新来的会员数(不包括facer)。以后n行每一行两个数,按照入会的时间给出会员的id和实力值。一开始,facer就算是会员,id为1,实力值1000000000。输入保证两人的实力值不同。
输出
N行,每行两个数,为每场比赛双方的id,新手的id写在前面。
思路
为了按如题的安排,且要减小复杂度,考虑到 S T L STL STL的 m a p map map容器,map中的 l o w e r _ b o u n d 和 u p p e r _ b o u n d lower\_bound和upper\_bound lower_bound和upper_bound。
其作用是按键值去找,因为 m a p map map内部其实是有序的。
找到方法后模拟题意,考虑为3中情况:
- 每个键值都大于等于他,那么直接输出
- 每个键值都小于等于他,直接输出
- 如果既不是 b e g i n begin begin也不是 e n d end end再特判一下就可以
#include<iostream>
#include<algorithm>
#include<list>
#include<set>
#include<map>
using namespace std;
map<int, int>mt;
int main() {
//ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; scanf("%d", &n);
int cnt = 1;
mt[1000000000] = 1;
map<int, int>::iterator it1, it2;
while (n--) {
int x, y; scanf("%d%d", &x, &y);
it1 = mt.lower_bound(y);
if (it1 == mt.begin()){
printf("%d %d\n", x, it1->second);
//cout << x << " " << it1->second << endl;
}
else if (it1 == mt.end()) {
--it1;
printf("%d %d\n", x, it1->second);
//cout<<x<<" "<<it1->second<<endl;
}
else {
int p1, p12;
int p2, p22;
p1 = it1->first, p12 = it1->second;
--it1;
p2 = it1->first, p22 = it1->second;
if (p1 - y < abs(p2 - y) ){
printf("%d %d\n", x, p12);
//cout << x << " " << p12 << endl;
}
else {
printf("%d %d\n", x, p22);
//cout << x << " " << p22 << endl;
}
}
mt[y] = x;
}
}