• 周五. 4 月 24th, 2026

物嫩软件资讯网

软件资讯来物嫩

066:冷血格斗场

admin@wunen

5 月 23, 2025

总时间限制: 1000ms 内存限制: 65536kB

描述

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

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

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

输入

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

输出

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

样例输入

3

2 3

3 1

4 2

样例输出

2 1

3 2

4 2


题意讲解:


这道题可以对照热血格斗场那题看(详见可见上一篇博客),不同的是,有几个细小的差别。

1.实力可以相同了!

2.输入数据的id不一定按照顺序输入

介于此,考虑用map<int ,set >来存储格斗场会员信息。对于实力相同的会员,存放于同一set(实现按id自动从小到大排序)中,并通过迭代器it 来访问其最小Id:

*(it->second.begin()); //此处it->second指向的是set,set只能用迭代器访问,不能通过下标

#include<map>
#include<iostream>
#include<algorithm>
#include<set>
#include<cstdlib>
using namespace std;
map<int,set<int> > club;
map<int,set<int> >::iterator it,t;
int n,id,power;
int main(){
	cin>>n;
	club[1000000000].insert(1);
	while(n--){
		cin>>id>>power;
		it=club.lower_bound(power);
		if(it==club.begin()){
			cout<<id<<" "<<*(it->second.begin())<<endl;
		}
		else if(it==club.end()){
			it--;
			cout<<id<<" "<<*(it->second.begin())<<endl;
		}
		else{
			int right=it->first;
			it--;
			int left=it->first;
			if(power-left<right-power){
				cout<<id<<" "<<*(it->second.begin())<<endl;
			}
			else if(power-left==right-power){
				t=it++;
				if(*(it->second.begin())<*(t->second.begin()))	cout<<id<<" "<<*(it->second.begin())<<endl;
				else	cout<<id<<" "<<*(t->second.begin())<<endl;
			}
			else{
				it++;
				cout<<id<<" "<<*(it->second.begin())<<endl;
			}
		}
		club[power].insert(id);
	}
	//system("pause");
	return 0;
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注