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 ...
随机推荐
- nested exception is java.lang.NoClassDefFoundError: net/sf/cglib/proxy/CallbackFilter
转自:https://blog.csdn.net/licheng989/article/details/28929411 在Bean中有代码 public abstract Axe getAxe(); ...
- SpringMVC之DispatcherServlet详解
SpringMVC是一种基于请求启动的WEB框架,并且使用了前端控制器的设计模式,所有满足[WEB-INF/web.xml]文件中的[url-pattern]的匹配条件的请求,这些满足的请求都会交给这 ...
- JavaScript中的+= 是什么?
+=表示相加并赋值,“i+=5”与“i=i+5”是等效的. 类似的运算符还有-=,*=,/=. i-=5等价于i=i-5; i*=5等价于i=i*5: i/=5等价于i=i/5.
- unity3D 使用欧拉角控制视野注意点
变量声明: public PlayerInput p; //表示控制代码用来获得用户是否按下 public float rotateSpeed = 50f; //旋转速度 private GameOb ...
- EasyUI DataGrid组织事件冒泡
在事件内部需要阻止的地方添加如下代码 ]; 解释: arguments.callee是获得我自定义的事件处理方法OnSelected的方法体. .caller是获得调用OnSelected的上层方法( ...
- 初学Hibernate杂乱总结
1.如果在"one"方中(如部门)写有Set属性,但是没有在映射文件中配置,那么,在获取指定部门下的所有员工时,不会报错,但是,Set内的元素个数为0.输出为"[]&qu ...
- javascript事件绑定1-模拟jquery可爱的东西
1.给对象添加事件attachEvent(兼容IE,不兼容ff.chrome) <html xmlns="http://www.w3.org/1999/xhtml"> ...
- Leetcode0100--Same Tree 相同树
[转载请注明]http://www.cnblogs.com/igoslly/p/8707664.html 来看一下题目: Given two binary trees, write a functio ...
- 《深入理解Android虚拟机内存管理》示例程序编译阶段生成的各种语法树完整版
1.tokens "int" "int" <SPACES> " &quo ...
- phpmyadmin搭建
phpadmin配置: 一.phpadmin安装及配置 1.解压phpadmin压缩包,并复制到 /usr/local/apache2/htdocs目录,重命名为dataManage 2.进入data ...