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>
#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(排序)

时间: 2024-11-05 06:13:18

uva:10763 - Foreign Exchange(排序)的相关文章

uva 10763 Foreign Exchange(排序+二分查找)

这道题是我第一次算出来应该用什么复杂度写的题,或许是这一章刚介绍过,500000的数据必须用nlogn,所以我就 想到了二分,它的复杂度是logn,再对n个数据进行遍历,正好是nlogn,前两次TLE了,然后我就对我的做法没信心 了...看到一篇博客上说就应该这种方法,然后我就坚定的改自己的算法去了,哈哈,专注度没有达到五颗星,最多 三颗... 思路: 我用的是结构体保存的,先对每一对序列排序,然后对第二个元素在第一个元素中二分搜索,用到了 lower_bound,upper_bound,注释里

紫书第五章训练 uva 10763 Foreign Exchange by crq

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

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

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

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

UVa 10763 (STL) Foreign Exchange

看到大神说location的值不会超过1000,所以这就简单很多了,用一个deg数组记录下来每个点的度,出度-1,入读+1这样. 最后判断每个点的度是否为0即可. 至于为什么会这样,据说是套数据套出来的,比如在代码里加一句if(a >= 1000) for(;;),get新技能! 如果按正常来做的话,我能想到的就是遍历map了. 1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 1000; 5 int

Uva 10305 给任务排序

题目链接:https://uva.onlinejudge.org/external/103/10305.pdf 紫书P167 拓扑排序. dfs——从一个点出发,dfs 与之相连的所有点,把本身放入到拓扑排序的首部. #include <bits/stdc++.h> using namespace std; const int Maxn = 1000; int G[Maxn][Maxn]; int topo[Maxn]; int c[Maxn]; int n,m,t; bool dfs(int

UVA 1423 - Guess(拓扑排序)

UVA 1423 - Guess 题目链接 题意:给定一个每个区间和的正负,构造一个序列,使得满足这个矩阵 思路:每个区间和等于两个前缀和的差,这样就可以知道每两个前缀和的大小关系,利用拓扑排序可以求出顺序,然后对应要控制不超过|10|,所以从-10开始,大的就+1,然后构造出这个前缀和序列,对应每个ai就等于c[i] - c[i - 1] 代码: #include <cstdio> #include <cstring> #include <vector> #inclu