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

冷血格斗场:

set<pair<int, int> > record;
void solve(int power, int id){
	set<pair<int, int> >::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<n; i++){
		int id, power;
		scanf("%d%d", &id, &power);
		record.insert(make_pair(power, id));
		solve(power, id);
	}
	//system("pause");
	return 0;
}

热血格斗场:

set<pair<int, int> > record;
void solve(int power, int id){
	set<pair<int, int> >::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<n; i++){
		int power, id;
		scanf("%d%d", &id, &power);		
		record.insert(make_pair(power, id));
		solve(power, id);
	}
	//system("pause");
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值