Foreign Exchange [UVA - 10763]

思路:能否成功取决于A->B的数量是否等于B->A的数量,那么只需要想办法判断每对 (A, B) 是否满足。

解决:
(1) 先确定每对 (A, B) 的表达形式。方法很多,我这里是存储每对 (A, B) 时,使 A, B 大小有序,则每一对 A, B 都变成了唯一形式, 可用结构体储存,按规则排序之后 (A, B) 就会相连,这样就便于处理 每一对(A, B) 是否满足。

(2) 确定每一对 (A, B) 是否满足。 给每个学生一个 val 值, 然后 使得A->B 与 B->A 的 val 值相反即可, (A, B) 的 所有val 值 加起来,等于0说明 (A, B) 这一对满足,否则整个情况就不满足。

整个算法复杂度O(n logn)

代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
//#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <set>
using namespace std;
//using namespace __gnu_cxx;

#define pair(a, b) make_pair(a, b)
#define memset(a, b) memset(a, b, sizeof a)
#define max(a, b) ((a) < (b) ? (b) : (a))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define fi first
#define se second

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0}; 

//typedef __int128 INT;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 5e5 + 10;
const int M = 2e5 + 10;
const int Mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int P = 13331;
const double D = 1e-8;
const double PI = acos(-1.0);

struct Node
{
	LL a, b;
	int val;
	bool operator < (const Node &W) const
	{
		if (a != W.a) return a < W.a;
		return b < W.b;
	}
	
	bool operator != (const Node &W) const
	{
		return a != W.a || b != W.b;
	}
} g[N];

int n, m, k;

int main()
{
	//freopen("C:\\Users\\86187\\Desktop\\stdin.txt", "r", stdin);
	
	while (cin >> n, n)
	{
		for (int i = 1; i <= n; i ++)
		{
			LL a, b;
			cin >> a >> b;
			int val = (a < b ? 1 : -1);
			if (a > b) swap(a, b);
			g[i] = {a, b, val};
		}
		
		sort(g + 1, g + 1 + n);
		
		int sum = 0;
		bool flag = true;
		for (int i = 1; i <= n; i ++)
		{
			if (i == 1 || g[i] != g[i - 1])
			{
				if (sum) break;
			}
			
			sum += g[i].val;
		}
		if (sum) flag = false;
		
		cout << (flag ? "YES" : "NO") << endl;
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值