043:冷血格斗场

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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值