题目: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(排序)的更多相关文章

  1. uva 10763 Foreign Exchange(排序比较)

    题目连接:10763 Foreign Exchange 题目大意:给出交换学生的原先国家和所去的国家,交换成功的条件是如果A国给B国一个学生,对应的B国也必须给A国一个学生,否则就是交换失败. 解题思 ...

  2. UVA 10763 Foreign Exchange 出国交换 pair+map

    题意:给出很多对数字,看看每一对(a,b)能不能找到对应的(b,a). 放在贪心这其实有点像检索. 用stl做,map+pair. 记录每一对出现的次数,然后遍历看看对应的那一对出现的次数有没有和自己 ...

  3. uva 10763 Foreign Exchange <"map" ,vector>

    Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enthu ...

  4. UVA 10763 Foreign Exchange

      Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description Your non- ...

  5. UVa 10763 Foreign Exchange(map)

    Your non-profitorganization (iCORE - international Confederationof Revolver Enthusiasts) coordinates ...

  6. 【UVA】10763 Foreign Exchange(map)

    题目 题目     分析 没什么好说的,字符串拼接一下再放进map.其实可以直接开俩数组排序后对比一下,但是我还是想熟悉熟悉map用法. 呃400ms,有点慢.     代码 #include < ...

  7. Foreign Exchange

     10763 Foreign ExchangeYour non-profit organization (iCORE - international Confederation of Revolver ...

  8. UVA Foreign Exchange

    Foreign Exchange Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Your non ...

  9. Foreign Exchange(交换生换位置)

     Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enth ...

随机推荐

  1. bzoj 1024 [ SCOI 2009 ] 生日快乐 —— 递归

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1024 因为每次把一块切成两块,所以可以枚举从哪里切开,然后递归求解: 一开始用了不太对的贪心 ...

  2. leetcode链表相关

    目录 2/445两数相加 综合题(328奇偶链表, 206反转链表, 21合并两个有序链表 ) 92反转链表 II 链表排序(148排序链表, 876链表的中间结点) 142环形链表 II 160相交 ...

  3. layui富文本编译器添加图片

    1.创建富文本编辑器 <form class="layui-form" method="post" id="myForm" encty ...

  4. TPshop规格组合错误

    TPshop规格组合错误 修改: admin/logic/goodslogic.class.php 中 方法:getSpecInput() 中 asort($spec_arr_sort) 去掉

  5. mysql行列转置

    --创建行转列表及插入数据 create table tb_RowConvertToColumn ( username nvarchar(100) null, course nvarchar(100) ...

  6. C - Football

    Problem description Petya loves football very much. One day, as he was watching a football match, he ...

  7. C#之密封类(详解)

    10.3  密封类与密封方法 如果所有的类都可以被继承,那么很容易导致继承的滥用,进而使类的层次结构体系变得十分复杂,这样使得开发人员对类的理解和使用变得十分困难,为了避免滥用继承,C#中提出了密封类 ...

  8. CSS自定义消息提示

    1.效果 2.源码 <%@ page contentType="text/html;charset=UTF-8" language="java" %> ...

  9. CSS清除浮动_清除float浮——详解overflow:hidden 与clear:both属性

    最近刚好碰到这个问题,看完这个就明白了.写的很好,所以转载了! CSS清除浮动_清除float浮动 CSS清除浮动方法集合 一.浮动产生原因   -   TOP 一般浮动是什么情况呢?一般是一个盒子里 ...

  10. Android Studio 将module打成jar包

    1.新建测试工程,工程里面有两个module,app是Android工程,mylibrary是Android Library库. 2.打开mylibrary目录下的build.gradle文件,加入下 ...