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 ...
随机推荐
- Tool:ProcessOn
ylbtech-Tool:ProcessOn ProcessOn是一个面向垂直专业领域的作图工具和社交网络,成立于2011年6月并于2012年启动.ProcessOn将全球的专家顾问.咨询机构.BPM ...
- Sorting It All Out(拓扑排序)
http://poj.org/problem?id=1094 1.判断所给关系是否为合法的拓扑序列,若存在环,输出 "Inconsistency found after %d relatio ...
- curl强制下载文件
<?phpfunction download_remote_file_with_curl($file_url, $save_to) { $ch = curl_init(); curl_setop ...
- web动画小结
前端写动画,无非两种方案,一种是通过css,另一种是js css的方案: 1.transform的单独使用 (IE9+) rotate(90deg) 2d旋转,也可以理解为沿着3D的Z轴旋转 rota ...
- epoll实现reactor模式
- php中curl的详细解说 【转载】
这几天在帮一些同学处理问题的时候,突然发现这些同学是使用file_get_contents()函数来采集页面内容的,貌似都没有curl的概念亦或是对这种工具特别不敏感, 本文我来给大家详细介绍下cUR ...
- Nmap linux端口扫描神器
#简介 Nmap亦称为Network Mapper(网络映射)是一个开源并且通用的用于Linux系统/网络管理员的工具.nmap用于探查网络.执行安全扫描.网络核查并且在远程机器上找出开放端口.它可以 ...
- ArrayList<HashMap<String,Object>>集锦
1. Android中如何从一个Activity中ArrayList<HashMap<String,Object>>传递到另一个activity? eg: ...
- ie8及其以下版本兼容性问题之响应式
解决办法:引入Respond.js让IE6-8支持CSS3 Media Query 使用方式 参考官方demo:http://scottjehl.github.com/Respond/test/tes ...
- CSS框架Bootstrap
作为一个软件开发人员,经常接触和使用框架是再平常的事情不过了.但是这些框架基本都是和语言相关的,比如WEB框架SpringMVC,JavaEE框架Spring,ORM框架Hibernate,还有Jav ...