shirobert 发表于 2015-9-11 10:46:14

uva 10763 Foreign Exchange(排序比较)

  题目连接:10763 Foreign Exchange
  

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

  解题思路:
  给出数据10
  xy
  
  1 2
  2 1
  3 4
  4 3
  100 200
  200 100
  57 2
  2 57
  1 2
  2 1
  

  按照排序:
  xy y x
  12 1 2
  12 1 2
  21 2 1
  21 2 1
  257 2 57
  34 3 4
  43 4 3
  572 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, rec;
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.x, &tmp.y);
rec.y = tmp.x;
rec.x = tmp.y;
}
sort(tmp, tmp + n, cmp);
sort(rec, rec + n, cmp);
int flag = 1;
for (int i = 0; i < n; i++)
if (tmp.x != rec.x || tmp.y != rec.y) {
flag = 0;
break;
}
printf("%s\n", flag ? "YES" : "NO");
}
return 0;
}

  
  
  
页: [1]
查看完整版本: uva 10763 Foreign Exchange(排序比较)