zoukankan      html  css  js  c++  java
  • uva:10763

    题目: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
    ");
    			continue;
    		}
    
    		sort (temp, temp + n, cmp);
    		sort (candidates, candidates + n, cmp);
    		printf ("%s
    ", judge()? "YES" : "NO");
    
    	}
    	return 0;
    }




  • 相关阅读:
    让网页活起来!韵律线带你提升带你飞!
    打造晶格化背景
    简单banner制作
    设计模式-适配器模式
    类、方法的单一职责
    .NET趋势
    C# Delegate Event
    VB.NET项目技术总结
    版本控制工具Git的使用
    delete语句要注意的BUG.
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7102852.html
Copyright © 2011-2022 走看看