uva:10763 - Foreign Exchange(排序)

题目:10763 - Foreign Exchange


题目大意:给出每个同学想要的交换坐标 a, b 代表这位同学在位置a希望能和b位置的同学交换。要求每一位同学都能找到和他交换的交换生。


解题思路:把给定的原先给定的序列,交换前后位置后得到新的序列。如果这个新的序列和原来的序列相同就符合要求。因为  (a,b) (b, a)若是成对出现,那么前后交换后还是(b, a)(a,b).


代码: 

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int N = 500005;
int n;
struct CANDIDATES {
	
	int x, y;
}candidates[N], temp[N];

bool cmp (const CANDIDATES &c1, const CANDIDATES &c2) {
	
	if (c1.x == c2.x)
		return c1.y < c2.y;
	return c1.x < c2.x;
}

bool judge () {

	for (int i = 0; i < n; i++) 
		if (candidates[i].y != temp[i].y  || temp[i].x != candidates[i].x)
			return false;
	return true;
}

int main () {

	int x, y;
	while (scanf ("%d", &n), n) {

		for (int i = 0; i < n; i++) {
			
			scanf ("%d%d", &x, &y);
			candidates[i].x = x;
			candidates[i].y = y;
				
			temp[i].x = y;
			temp[i].y = x;
		}
		
		if (n % 2) {                  //这里要注意,不能写到输入数据的前面,因为这个好多次的TL
			
			printf ("NO\n");
			continue;
		}

		sort (temp, temp + n, cmp);
		sort (candidates, candidates + n, cmp);
		printf ("%s\n", judge()? "YES" : "NO");

	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值