uva: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;
}
uva:10763 - Foreign Exchange(排序)的更多相关文章
- uva 10763 Foreign Exchange(排序比较)
题目连接:10763 Foreign Exchange 题目大意:给出交换学生的原先国家和所去的国家,交换成功的条件是如果A国给B国一个学生,对应的B国也必须给A国一个学生,否则就是交换失败. 解题思 ...
- UVA 10763 Foreign Exchange 出国交换 pair+map
题意:给出很多对数字,看看每一对(a,b)能不能找到对应的(b,a). 放在贪心这其实有点像检索. 用stl做,map+pair. 记录每一对出现的次数,然后遍历看看对应的那一对出现的次数有没有和自己 ...
- uva 10763 Foreign Exchange <"map" ,vector>
Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enthu ...
- UVA 10763 Foreign Exchange
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description Your non- ...
- UVa 10763 Foreign Exchange(map)
Your non-profitorganization (iCORE - international Confederationof Revolver Enthusiasts) coordinates ...
- 【UVA】10763 Foreign Exchange(map)
题目 题目 分析 没什么好说的,字符串拼接一下再放进map.其实可以直接开俩数组排序后对比一下,但是我还是想熟悉熟悉map用法. 呃400ms,有点慢. 代码 #include < ...
- Foreign Exchange
10763 Foreign ExchangeYour non-profit organization (iCORE - international Confederation of Revolver ...
- UVA Foreign Exchange
Foreign Exchange Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Your non ...
- Foreign Exchange(交换生换位置)
Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enth ...
随机推荐
- Codeforces--621B--Wet Shark and Bishops(数学)
B. Wet Shark and Bishops time limit per test 2 seconds memory limit per test 256 megabytes input ...
- C Tricks(十六)—— 复制字符串
while (*s++ = *t++); // target ⇒ source // 对于 C 语言而言,赋值运算符返回左值
- 第13课 SmartGit程序操作介绍
http://www.syntevo.com/
- vs code golang代码自动补全
“go.useCodeSnippetsOnFunctionSuggest”: true 文件-->首选项--->设置--->用户设置 添加下行:然后就可以自动补全了,包括() “go ...
- 使用 Polyfill 而不再是 bable 来实践js新特性
现状 我们想要用ES6 语法来写 JavaScript.然而由于我们需要兼容老版本的浏览器,那些浏览器不支持 ES6,我们需要解决这个问题. 有一个标准的做法是:写 ES6 代码 → 将所有代码编译成 ...
- facade 模式和gateway模式的区别
原文:http://stackoverflow.com/questions/4422211/what-is-the-difference-between-facade-and-gateway-desi ...
- 数据库操作通用函数,增强可重复利用性能C#,asp.net.sql2005
using System;using System.Data;using System.Data.SqlClient; namespace com.hua..li{ /// <summary&g ...
- JavaScript中赋值运算符的使用
JavaScript中的赋值运算可以分为简单赋值运算和复合赋值运算.简单赋值运算是将赋值运算符(=)右边表达式的值保存到左边的变量中:而复合赋值运算混合了其他操作(例如算术运算操作)和赋值操作.例如: ...
- hibernate_02_session介绍
什么是session? hibernate的session对象就相当于jdbc的connection.我们对数据库的操作(增删改等)都是使用的session方法. 写一个java类 package c ...
- macOS下登录store或者xcode等应用时提示【this action could not be completed】
sudo mkdir -p /Users/Shared sudo chown root:wheel /Users/Shared sudo chmod -R 1777 /Users/Shared === ...