c ++ Programming Exercises 042: Blood fighting games

North programming and algorithms (c) quiz summary (Spring 2020)


description

In order to meet the 2008 Olympic Games, so that we better understand the various combat sports, facer opened a new blood fighting games. Fighting games membership system, but the new members do not need to pay the entrance fee, as long as playing an exhibition match with an old members, to prove their strength.

We assume that the fighting strength can be a positive integer, the value becomes strength. In addition, each person has a unique id, it is a positive integer. In order to make the game look better, each player will choose a new strength with his closest human race, that is the absolute value of the difference between the value of the strength of both teams as small as possible, if two people have the strength value of the difference with him the same, he will choose his weaker than that (apparently, the abused child who will be good).

Unfortunately, Facer accidentally lost the race record, but he retains records registered members. Now would you please help facer resume playing record, in chronological order of the output of each game between the two sides id.

Enter
the first line of a number n (0 <n <= 100000 ), the fighting field indicates the number of new members (not including facer). N each row after row of two numbers, given the strength of the id and the value of membership in accordance with the membership of time. A beginning, facer even a member, id is 1, the strength values of 1 billion. To ensure that different strength of the two input values.

Output
N lines of two numbers, both the id, id novice EDITORIAL per game.

Sample input

3
2 1
3 3
4 2

Sample Output

2 1
3 2
4 2

Directly on the code

#include<iostream>
#include<map>
using namespace std;
int main()
{
	int id,power,num;
	multimap<int ,int> data;
	data.insert(make_pair(1000000000,1));//第一个数据 
	cin >> num;
	while(num --)
	{
		cin >> id >> power;
		multimap<int ,int>::iterator im,in,i,j;
		im = data.insert(make_pair(power,id));
		if(im == data.begin()){//判断是否是第一个元素
			in = im;
			in++;
		 	cout<<im->second <<" "<< in->second << endl;
		}
		else if(im == (--data.end())){//判断是否是最后一个元素
			in = im;
			--in;
			cout<<im->second <<" "<< in->second << endl;
		}
		else{
			i = im;
			--i;
			j = im;
			++j;
			if(im->first - i->first > (j->first - im->first))
				cout<<im->second <<" "<< j->second<<endl;
			else
				cout<<im->second <<" "<< i->second<<endl;
		}
		
	}
}
 
Published 222 original articles · won praise 48 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_44116998/article/details/104419768