现在的位置: 首页 > 综合 > 正文

uva 10763 Foreign Exchange(排序比较)

2013年12月03日 ⁄ 综合 ⁄ 共 936字 ⁄ 字号 评论关闭

题目连接:10763 Foreign Exchange

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


解题思路:

给出数据  10

x  y

1

2

3

4

100 200 

200
100 

57

2
57 

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;
}

抱歉!评论已关闭.