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 ...
随机推荐
- B1060 [ZJOI2007]时态同步 dfs
两遍dfs,第一遍有点像找重链,第二遍维护答案,每个点维护一个当前深度,然后就没啥了. ps:memset(lst,-1,sizeof(lst));这一句多余的话让我debug半天... 题干: De ...
- 【转】iOS开发-关闭/收起键盘方法总结
原文网址:http://www.cnblogs.com/GarveyCalvin/p/4167759.html 前言:作为IOS开发人员,需要经常和表单打交道.因此我对收起键盘的方法作了下总结,IOS ...
- expectation-maximization algorithm ---- PRML读书笔记
An elegant and powerful method for finding maximum likelihood solutions for models with latent varia ...
- 使用Web Workers处理线程
使用HTML 4和JavaScript创建出来的Web程序中,因为所有的处理都是在单线程中 HTML 5的Web Workers API,HTML 5中,一个Worker实际上为一个后台运行的线程.
- 前端面试准备之JavaScript
1.数据类型. JavaScript中有5种简单数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number和String.还有1种复杂数据类型——Object,Obje ...
- windows php文件下载地址
http://windows.php.net/downloads/releases/archives/
- windbg将调试信息保存到文本文件
在跟踪一些出现频率较低的问题时,有时候需要长时间调试,但是在在输出信息太多时可能前面的日志会被清空,为避免这种情况,可以将输出日志记录到文本文件以备查看. 1. 可以在启动时直接用带 -logo的命令 ...
- java中equals方法和==的用法
java中equals方法的用法以及==的用法(参考一)equals 方法是 java.lang.Object 类的方法.两种用法说明:(1对于字符串变量来说,使用“==”和“equals()”方法比 ...
- iconfont在ios(safari)中的坑
最近公司决定将项目图标整体迁移到iconfont,按网上常规方法,在安卓.pc端都没问题,唯独在ios的safari浏览器及微信内置浏览器中,iconfont始终在正常位置向下偏移,导致图标错乱. 网 ...
- classNum 表示学生的班号,例如“class05”。 有如下List List list = new ArrayList();
package a927; import java.util.ArrayList; import java.util.List; class Student { private String name ...