uva:10763 - Foreign Exchange(排序)
2014-07-07 22:24
363 查看
题目:10763 - Foreign Exchange
题目大意:给出每个同学想要的交换坐标 a, b 代表这位同学在位置a希望能和b位置的同学交换。要求每一位同学都能找到和他交换的交换生。
解题思路:把给定的原先给定的序列,交换前后位置后得到新的序列。如果这个新的序列和原来的序列相同就符合要求。因为 (a,b) (b, a)若是成对出现,那么前后交换后还是(b, a)(a,b).
代码:
题目大意:给出每个同学想要的交换坐标 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 , temp ; 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; }
相关文章推荐
- UVA - 10763 Foreign Exchange(排序)
- uva 10763 Foreign Exchange(排序比较)
- uva 10763 Foreign Exchange(排序比较)
- UVA 10763 - Foreign Exchange【排序】
- uva:10763 - Foreign Exchange(排序)
- UVa 10763 - Foreign Exchange(结构体排序)
- uva 10420 List of Conquests(字符串排序)
- UVA - 10763 Foreign Exchange
- 大理石在哪儿 (排序和查找) UVA - 10474
- UVa-10763 交流生
- (使用STL自带的排序功能进行排序)UVA 10905 Children's Game(求一组数据所能拼出的最大数字串)
- uva 10785(排序、检索)
- UVa 10905 Children's Game 解题报告(排序)
- UVa 11462 年龄排序 (计数排序及IO优化)
- uva 10763 Foreign Exchange
- uva 10954 Add All(排序)
- uva 10905 Children's Game(排序或者有点贪心)
- uva - 10763 Foreign Exchange(贪心)
- UVA 755 (暑假-排序、检索(2)-D-487--3279 )
- UVa 10763