Cold-blooded and warm-blooded Fighting games Fighting games

Good bar, long past the title, and now make up out of. Grudges with fighting games. Each submitted a dozen times.
First pick what topics.

5: Blood fighting games
are described
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

At first I was modeled after God's Guo student information entry and inquiry system did write, that is, each cycle again map to find strength and its closest. And then I found it wrong title, ID, and join independent of time, the sample data too confusing up. This requires each entry are processed. The second is, this time will be over. Therefore, we can use another heavy cycle, and need to be processed with the STL lower_bound and upper_bound.
But still write hung up, perhaps at night his mind is not clear.
Note that the point is, if the return is lower_bound begin, then that is no more than the membership of the dish, which is itself begin, you do not need more direct output upper_bound, this is actually a very mentally conclusion but I was silly ...... probably two kinds of ideas about the conversion did not come.
Then I had also added a while ...... emmm do not know what was in the doing.
Finally, AC codes:

#include <iostream>
#include <map>
#include <cmath>
#include <algorithm>
#include <stdio.h>
using namespace std;
class member {
public:
	int force;
	struct info {
		int order;
		int id;
	};
	info data;
};
typedef map<int, int> MAP;
int main() {
	MAP record;
	member A;
	int n;
	scanf("%d", &n);
	A.data.id = 1;
	A.data.order = 0;
	A.force = 1000000000;
	record.insert(MAP::value_type(A.force, A.data.id));
	for (int i = 1; i <= n; i++) {
		A.data.order = i;
		scanf("%d%d", &A.data.id, &A.force);
		record.insert(MAP::value_type(A.force, A.data.id));
		int mindis = 1000000000;
		typename MAP::iterator pp1 = record.lower_bound(A.force);
		typename MAP::iterator pp2 = record.upper_bound(A.force);
		if (pp1 == record.begin()){
			printf("%d %d\n", A.data.id, pp2->second);
			continue;
		}
		--pp1;
		printf("%d %d\n", A.data.id, (abs(A.force - pp1->first) > abs(A.force - pp2->first) ? pp2->second : pp1->second));
	}
	return 0;
}

Fighting games seem cold-blooded harder, I made reference to someone else's code was AC.
First on the topic:

3: The cold-blooded fighting games
overall time limit: 1000ms Memory Limit: 65536kB
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

First on AC Code:

# include <iostream>
# include <map>
using namespace std;
typedef map <int, int> MYMAP;
struct member {
	int force;
	int id;
};
int main () {
	int n;
	cin >> n;
	member A;
	MYMAP record;
	record.insert (MYMAP::value_type (1000000000, 1));
	for (int i = 0; i < n; i++) {
		scanf ("%d%d", &A.id, &A.force);
		typename MYMAP::iterator p1 = record.lower_bound (A.force);
		typename MYMAP::iterator p2 = record.upper_bound (A.force);
		MYMAP::iterator low = record.lower_bound (A.force);
		MYMAP::iterator up = record.upper_bound (A.force);
		if (low == record.end()) {
			--low;
			printf ("%d %d\n", A.id, low->second);
		}
		else if (low == record.begin()) {
			printf ("%d %d\n", A.id, low->second);
		}
		else if (low->first == A.force){
			printf ("%d %d\n", A.id, low->second);
		}
		else {
			--low;
			if (A.force - low->first < up->first - A.force) {
				printf ("%d %d\n", A.id, low->second);
			}
			else if (A.force - low->first > up->first - A.force) {
				printf ("%d %d\n", A.id, up->second);
			}
			else {
				printf ("%d %d\n", A.id, (low->second < up->second? low->second: up->second));
			}
		}
		if (record.find(A.force) != record.end()) {
			record[A.force] = min(A.id, record[A.force]);
		}
		else record.insert (MYMAP::value_type (A.force, A.id));
	}
	return 0;
}

The first pass when I innocently retained all the information and then traverse to find the smallest ID, then hung up (in fact, feel a little lengthy in addition to thinking outside no problem but it is not over)
the second time when I code AC basis, each dispersed in the insert if-else where, the last if-else deleted, and then hung up.
Well I do not really know why, from a logical point of view there is no problem ...... a solution to a problem is not qualified, placed here to remind ourselves that the code should be simple, concise, simple and good.

Guess you like

Origin blog.csdn.net/weixin_44288817/article/details/89840811