uva 10763 Foreign Exchange(排序比较)

737 篇文章 0 订阅
232 篇文章 0 订阅

题目连接:10763 Foreign Exchange


题目大意:给出交换学生的原先国家和所去的国家,交换成功的条件是如果A国给B国一个学生,对应的B国也必须给A国一个学生,否则就是交换失败。


解题思路:

给出数据  10

x  y

1

2

3

4

100 200 

200100 

57

257 

1

2


按照排序:

x x

1 2

1 2

2 1

2 1

2 57 57  

3

4 3

57 57 2

100 200 100 200

200 100 200 100

如果两个序列相同的话,说明交换成功,因为对应x = 1 , y = 2时,按照x, y 的大小排序,这对数字应该放在第一位,如果存在x = 2, y = 1,按照y,x的大小排序,也应该放在第一位。


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 500005;

struct que {
    int x;
    int y;
}tmp[N], rec[N];

bool cmp(const que &a, const que &b) {
    if(a.x < b.x)
	return true;
    else if (a.x > b.x)
	return false;
    else if (a.y < b.y)
	return true;
    else
	return false;
}

int main() {
    int n;
    while (scanf("%d", &n), n) {
	// Init;
	memset(tmp, 0, sizeof(tmp));
	memset(rec, 0, sizeof(rec));

	// Read;
	for (int i = 0; i < n; i++) {
	    scanf("%d%d", &tmp[i].x, &tmp[i].y);
	    rec[i].y = tmp[i].x;
	    rec[i].x = tmp[i].y;
	}

	sort(tmp, tmp + n, cmp);
	sort(rec, rec + n, cmp);

	int flag = 1;
	for (int i = 0; i < n; i++)
	    if (tmp[i].x != rec[i].x || tmp[i].y != rec[i].y) {
		flag = 0;
		break;
	    }
	printf("%s\n", flag ? "YES" : "NO");
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值