Foreign Exchange(交换生换位置)

           Foreign Exchange

Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very successful foreign student exchange program. Over the last few years, demand has sky-rocketed and now you need assistance with your task. The program your organization runs works as follows: All candidates are asked for their original location and the location they would like to go to. The program works out only if every student has a suitable exchange partner. In other words, if a student wants to go from A to B, there must be another student who wants to go from B to A. This was an easy task when there were only about 50 candidates, however now there are up to 500000 candidates!

Input

The input file contains multiple cases. Each test case will consist of a line containing n – the number of candidates (1 ≤ n ≤ 500000), followed by n lines representing the exchange information for each candidate. Each of these lines will contain 2 integers, separated by a single space, representing the candidate’s original location and the candidate’s target location respectively. Locations will be represented by nonnegative integer numbers. You may assume that no candidate will have his or her original location being the same as his or her target location as this would fall into the domestic exchange program. The input is terminated by a case where n = 0; this case should not be processed.

Output

For each test case, print ‘YES’ on a single line if there is a way for the exchange program to work out, otherwise print ‘NO’.

Sample Input

10

1 2

2 1

3 4

4 3

100 200

200 100

57 2

2 57

1 2

2 1

10

1 2

3 4

5 6

7 8

9 10

11 12

13 14

15 16

17 18

19 20

0

Sample Output

YES

NO

题解:开两个数组s【i】,t【i】,然后把数组数据从小到大排序,循环如果能以一一对应相等,则说明可以交换,输出YES,否则只要有一个不相等就输出NO.

#include<iostream>
#include<algorithm>
using namespace std;
int s[500000],t[500000];//注意数组大小
int main()
{
  int n,i;
  while(cin>>n&&n)
{
  for(i=0; i<n; i++)
    cin>>s[i]>>t[i];
  sort(s,s+n);//排序
  sort(t,t+n);
  int f=0;
  for(i=0; i<n; i++)
{
    if(s[i]!=t[i])
  {
      f=1;
      break;
  }
}

if(f)
    cout<<"NO"<<endl;
else
    cout<<"YES"<<endl;
}
return 0;

}

时间: 2024-10-13 08:54:06

Foreign Exchange(交换生换位置)的相关文章

uva:10763 - Foreign Exchange(排序)

题目:10763 - Foreign Exchange 题目大意:给出每个同学想要的交换坐标 a, b 代表这位同学在位置a希望能和b位置的同学交换.要求每一位同学都能找到和他交换的交换生. 解题思路:把给定的原先给定的序列,交换前后位置后得到新的序列.如果这个新的序列和原来的序列相同就符合要求.因为  (a,b) (b, a)若是成对出现,那么前后交换后还是(b, a)(a,b). 代码: #include <stdio.h> #include <string.h> #inclu

Foreign Exchange

 10763 Foreign ExchangeYour non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinatesa very successful foreign student exchange program. Over the last few years, demand hassky-rocketed and now you need assistan

UVA Foreign Exchange

Foreign Exchange Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very successful foreign student exchange program. Over th

Android - 交换控件位置:基于LayoutParams的瞬间交换与基于ObjectAnimator动画效果交换

现需要交换两个控件(本文中是两个RelativeLayout),找到了两个方法: 1.使用LayoutParams改变两个layout的属性,即其相对关系(below等),实现位置的交换,但是并没有交换的动画效果,是"瞬间"交换. 2.使用animation交换控件位置,实现了我需要的动画效果. 如下图,交换layoutOne 与layoutTwo . 一.首先介绍使用LayoutParams的方法. <span style="font-size:18px;"

ACM交换生问题

10763 Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very successful foreign student exchange program. Over the last few years, demand has sky-rocketed and now you need assist

【题目荟萃】换位置游戏

换位置游戏 时间限制: 1 Sec 内存限制: 128 MB 题目描述 N 个小朋友(编号为 1 到 N)正在玩一个换位置游戏.从左到右依次排列着 N 个凳子 (编号为 1 到 N,最左边的为 1 号凳子,最右边的为 N 号凳子),每个凳子上都有一个数字 (凳脚处红色数字),每个数字互不相同,且都是不超过 N 的正整数. 每轮游戏每个小朋友会做到凳子所写的数字的编号的凳子,求最小轮数让所有人坐到跟自己编号一样的凳子: 输入 输入共 2 行. 第 1 行是一个整数 N(1≤N≤1000),表示参加

uva 10763 Foreign Exchange

给定n对信息,是1-->2有一对交换生,能交换的条件是2-->1也有一对交换生,问能否顺利交换. 思路:用有向图模拟,如果1-->2有一对,那么就优先判断2-->1有没人交换,有的话,就不用加边了,直接标记那条边用了即可. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using n

换位置,就该换个性

之前我们聊过<不要用战术上的勤奋掩盖战略上的懒惰>,大概表达的意思有:在其位谋其职的意思,今天智云通CRM系统小编要跟大家分享某CEO关于"换位置,就该换个性"的话题. 我部门里有位经理,其实在我进公司时她已经是个主管了,我问她过几年要不要升协理,她说要,我说:「那妳就做一个经理的样子给我看.」她愣住了,因为她一直做自己做得很好.她从小是乖乖牌,听父母.老师的话,学历好.头脑好,进了职场前几年也都听话得不得了,表现十分优异,但等到被拉拔成经理时,却还是像以前一样顺从度高,从

如何把StringBuilder类型字符串中的字符换位置

首先,没有直接的方法可以使StringBuilder类型中的字符换位置.可以使用的方法如下: StringBuilder sb = new StringBuilder(s);      char x = sb.charAt(0); char y = sb.charAt(3); sb.setCharAt(0, y); sb.setCharAt(3, x); //做到了把位置在0和3的字符互换了位置.