043:冷血格斗场

it2025-04-02  10

043:冷血格斗场

题面

描述

为了迎接08年的奥运会,让大家更加了解各种格斗运动,facer新开了一家冷血格斗场。格斗场实行会员制,但是新来的会员不需要交入会费,而只要同一名老会员打一场表演赛,证明自己的实力。

我们假设格斗的实力可以用一个非负整数表示,称为实力值,两人的实力值可以相同。另外,每个人都有一个唯一的id,也是一个正整数。为了使得比赛更好看,每一个新队员都会选择与他实力最为接近的人比赛,即比赛双方的实力值之差的绝对值越小越好,如果有多个人的实力值与他差别相同,则他会选择id最小的那个。

不幸的是,Facer一不小心把比赛记录弄丢了,但是他还保留着会员的注册记录。现在请你帮facer恢复比赛纪录,按照时间顺序依次输出每场比赛双方的id。

输入

第一行一个数n(0 < n <=100000),表示格斗场新来的会员数(不包括facer)。以后n行每一行两个数,按照入会的时间给出会员的id和实力值。一开始,facer就算是会员,id为1,实力值1000000000。

输出

N行,每行两个数,为每场比赛双方的id,新手的id写在前面。

思路

基本同热血冷斗场,唯一的区别是如果有多个人实力值与他差别相同,他会选择id最小的那个。

所以我们只需要确定每个实力值相同的键值都保证id是最小。

#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 { if(p1 - y == abs(p2 - y) ){ printf("%d %d\n",x,min(p12,p22)); } else printf("%d %d\n", x, p22); //cout << x << " " << p22 << endl; } } if(mt.find(y)!=mt.end()){ mt[y]=min(mt[y],x); } else mt[y]=x; } }
最新回复(0)