NOI 3343 NOI3344 cold-blooded passion and fighting games

Note do first blood fighting games, the same cold-blooded thinking.

Source title

(OPENJUDGE) NOI3343: Blood fighting games http://noi.openjudge.cn/ch0309/3343/

Total 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 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.
 

Idea: build strength and id mapping map, because the map with automatic sorting features, so every time a new reading of the strength of the players and the id, time and insert the map, automatically sorted. Then as long as the position of the current players in the map to find, the front element and a rear element must be the smallest element value of the difference between the strength. Note borders: the current players could be begin, not a former player, or end-1, not after a player.

Code is as follows: rs record the final result, in the middle of a lot of input output comment because I find printf ( "% ld% ld", id, val), this sentence does not help, you must write separately, or the second total output number 0. 

/*
描述

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

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

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

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

输出
N行,每行两个数,为每场比赛双方的id,新手的id写在前面。
---------------------
使用map,key=实力,value是id,每次加入一个新的选手自动按key排序,使得最接近的就是相邻的两个选手。
*/

#include<iostream>
#include<map>
using namespace std;
typedef long long LL;
int main() {
	map<LL, LL> member;
	member.insert(make_pair(1000000000, 1));
	LL n = 0;
	//cin >> n;
	scanf("%ld", &n);
	//cout << n << endl;
	
	LL* rs = new LL[2 * n];
	for (LL i = 0; i < n; i++) {
		LL id = 0;
		LL val = 0;
		scanf("%ld %ld", &id, &val);
		//scanf("%d", &id);
		//cout << "id id" << id << endl;
		//scanf("%d", &val);
		//cout << "val is " << val << endl;
		//cin >> id;
		//cin>>val;
		//printf("-----%ld ", id);
		//printf("%ld \n", val);
	//	cout << "id id" << id << "  val is " << val << endl;
		rs[2 * i] = id;
		member.insert(make_pair(val, id));
		
		map<LL, LL>::iterator iter = member.find(val);
		if (iter == member.begin()) {
			iter++;
			int p2 = iter->second;
			rs[2 * i + 1] = p2;
			continue;
		}
		iter--;
		int p1 = (iter)->second;
		int s1 = (iter)->first;
		iter++;
		iter++;
		if (iter == member.end()) {
			rs[2 * i + 1] = p1;
			continue;
		}
		int p2 = (iter)->second;;
		int s2= (iter)->first;
		if (abs(val - s1) <= abs(val - s2)) {
			rs[2 * i + 1] = p1;
		}
		else {
			rs[2 * i + 1] = p2;
		}

	}
	for (LL i = 0; i <  n; i++) {
		printf("%ld ", rs[2 * i]);
		printf("%ld \n", rs[2 * i + 1]);
	}
	//system("pause");
	return 0;
	
}

Cold-blooded fighting games is actually similar ideas, the key is that if there are multiple with the same absolute value, select id small one.

Direct search map before insertion. If the strength of the players already exists, it is not inserted, and the players directly against the (absolute value of the difference is zero because, certainly, and the people playing, and id is the smallest (in the next two-line interpreter)),

Otherwise insert, field operations and blood the same execution.

Since the input to ensure that the id of small to large order, so if there is the strength of the players, do not insert to ensure the same map the strength of the players is only one, and always the smallest id

code show as below:

#include<iostream>
#include<map>
using namespace std;
typedef long long LL;
int main() {
	map<LL, LL> member;
	member.insert(make_pair(1000000000, 1));
	LL n = 0;
	//cin >> n;
	scanf("%ld", &n);
	//cout << n << endl;

	LL* rs = new LL[2 * n];
	for (LL i = 0; i < n; i++) {
		LL id = 0;
		LL val = 0;
		scanf("%ld %ld", &id, &val);
		//scanf("%d", &id);
		//cout << "id id" << id << endl;
		//scanf("%d", &val);
		//cout << "val is " << val << endl;
		//cin >> id;
		//cin>>val;
		//printf("-----%ld ", id);
		//printf("%ld \n", val);
		//	cout << "id id" << id << "  val is " << val << endl;
		rs[2 * i] = id;
		map<LL, LL>::iterator iter = member.find(val);
		if (iter == member.end()) {
			member.insert(make_pair(val, id));
			iter = member.find(val);
			if (iter == member.begin()) {
				iter++;
				int p2 = iter->second;
				rs[2 * i + 1] = p2;
				continue;
			}
			iter--;
			int p1 = (iter)->second;
			int s1 = (iter)->first;
			iter++;
			iter++;
			if (iter == member.end()) {
				rs[2 * i + 1] = p1;
				continue;
			}
			int p2 = (iter)->second;;
			int s2 = (iter)->first;
			if (abs(val - s1) <= abs(val - s2)) {
				rs[2 * i + 1] = p1;
			}
			else {
				rs[2 * i + 1] = p2;
			}
		}
		else {
			rs[2 * i + 1] = iter->second;
			continue;
		}
	}
	for (LL i = 0; i < n; i++) {
		printf("%ld ", rs[2 * i]);
		printf("%ld \n", rs[2 * i + 1]);
	}
	//system("pause");
	return 0;

}

 

Published 32 original articles · won praise 5 · Views 4661

Guess you like

Origin blog.csdn.net/qq_38941327/article/details/89312750