c ++ Programming Exercises 043: cold-blooded 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 cold-blooded 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, strength of the two values ​​can be the same. 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, with his personal strength value if the same difference how, then he will choose the id of the smallest.

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.

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

Sample input
. 3
2. 3
. 3. 1
. 4 2
Sample Output
2. 1
. 3 2
. 4 2


analysis

The question needs to be noted that the fighting strength can be a positive integer, the value becomes strength, strength of the two values are the same, you can choose to multimapimplement data storage; and because of the presence of people of the same strength, then we after each comparison, may be the same strength, but leave the smaller number, the larger number were deleted, because it is not deleted, subsequent operations, may choose not to meet the conditions of topics: personal strength values with the same difference if how he is he will choose the id of the smallest.

#include<iostream>
#include<map>
using namespace std;
multimap<int ,int> data;
void Delete_max(multimap<int ,int>::iterator i,multimap<int ,int>::iterator j)
{
	if(i->first == j->first){//id相同的 
		if(i->second < j->second)//去掉较大值 
			data.erase(j);
		else
			data.erase(i);
	}
}
int main()
{
	int id,power,num;
	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));
		i = im,--i;
		j = im,++j;
		if(im == data.begin()){
			in = im;
			in++;
		 	cout<<im->second <<" "<< in->second << endl;
		 	Delete_max(im,in);
		}
		else if(im == (--data.end())){
			in = im;
			--in;
			cout<<im->second <<" "<< in->second << endl;
			Delete_max(im,in);
		}
		else{
			int d1 = abs(im->first - i->first);
			int d2 = abs(im->first - j->first);
			if(d1 < d2  || d1 == d2 && i->second < j->second){
				cout<<im->second <<" "<< i->second<<endl;
				Delete_max(im,i);
			}
			else{
				cout<<im->second <<" "<< j->second<<endl;
				Delete_max(im,j);
			}
		}
		
	}
}
 
Published 222 original articles · won praise 48 · views 20000 +

Guess you like

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