【stl set】冷血格斗场 && 热血格斗场

冷血格斗场:

set > record;
void solve(int power, int id){
	set >::iterator ptr, left, right;
	ptr = record.find(make_pair(power, id));
	right = record.end(); right--;
	if(ptr == record.begin()){
		right = ptr;
		right++;
		printf("%d %d\n", id, right->second);
		
		// id is already sorted
		if((power == right->first))
			record.erase(right);
		//else if((power == right->first) && (right->first < id))
		//	record.erase(ptr);
	}
	else if(ptr == right){
		left = ptr; left--;
		printf("%d %d\n", id, left->second);
		
		if((power == left->first))
			record.erase(left);
		//else if((power == left->first) && (left->first < id))
		//	record.erase(ptr);
	}
	else{
		left = ptr; left--;
		right = ptr; right++;
		if((right->first - ptr->first)<(ptr->first - left->first))
			printf("%d %d\n", id, right->second);
		else if((right->first - ptr->first)>(ptr->first - left->first))
			printf("%d %d\n", id, left->second);
		else{
			// compare id
			if(right->second < left->second)
				printf("%d %d\n", id, right->second);
			else
				printf("%d %d\n", id, left->second);
		}
		
		// gurantee power is unique in the whole set
		if(power == right->first)
			record.erase(right);
		if(power == left->first)
			record.erase(ptr);
	}
	return;
}

int main(){		
	int n;
	scanf("%d", &n);
	record.insert(make_pair(1000000000, 1));
	for(int i=0; i

热血格斗场:

set > record;
void solve(int power, int id){
	set >::iterator ptr, fo, ba;	
	ptr = record.find(make_pair(power, id));
	fo = record.end();
	fo--;
	if(ptr == record.begin()){
		fo = ptr;
		fo++;
		printf("%d %d\n", ptr->second, fo->second);
	}
	// else if(ptr == record.end())
	// actuallt end() does not point to any valid element
	// so it is a potential problem, but test data is too weak
	else if(ptr == fo){
		ba = ptr;
		ba--;
		printf("%d %d\n", ptr->second, ba->second);
	}
	else{
		fo = ptr; fo++;
		ba = ptr; ba--;
		if((fo->first - ptr->first)<(ptr->first - ba->first))
			printf("%d %d\n", ptr->second, fo->second);
		else
			printf("%d %d\n", ptr->second, ba->second);
	}
	return;
}

int main(){		
	int n;
	scanf("%d", &n);
	record.insert(make_pair(1000000000, 1));
	for(int i=0; i


你可能感兴趣的:(ACM_POJ)