zoukankan      html  css  js  c++  java
  • uva 10763 Foreign Exchange(排序比较)

    题目连接:10763 Foreign Exchange


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


    解题思路:

    给出数据  10

    x  y

    1 2 

    2 1 

    3 4 

    4 3 

    100 200 

    200 100 

    57 2 

    2 57 

    1 2 

    2 1 


    按照排序:

    xy y x

    12 1 2

    12 1 2

    21 2 1

    21 2 1

    257 2 57  

    34 3 4 

    43 4 3

    572 57 2

    100 200 100 200

    200 100 200 100

    如果两个序列相同的话,说明交换成功,因为对应x = 1 , y = 2时,按照x, y 的大小排序,这对数字应该放在第一位,如果存在x = 2, y = 1,按照y,x的大小排序,也应该放在第一位。


    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    const int N = 500005;
    
    struct que {
        int x;
        int y;
    }tmp[N], rec[N];
    
    bool cmp(const que &a, const que &b) {
        if(a.x < b.x)
    	return true;
        else if (a.x > b.x)
    	return false;
        else if (a.y < b.y)
    	return true;
        else
    	return false;
    }
    
    int main() {
        int n;
        while (scanf("%d", &n), n) {
    	// Init;
    	memset(tmp, 0, sizeof(tmp));
    	memset(rec, 0, sizeof(rec));
    
    	// Read;
    	for (int i = 0; i < n; i++) {
    	    scanf("%d%d", &tmp[i].x, &tmp[i].y);
    	    rec[i].y = tmp[i].x;
    	    rec[i].x = tmp[i].y;
    	}
    
    	sort(tmp, tmp + n, cmp);
    	sort(rec, rec + n, cmp);
    
    	int flag = 1;
    	for (int i = 0; i < n; i++)
    	    if (tmp[i].x != rec[i].x || tmp[i].y != rec[i].y) {
    		flag = 0;
    		break;
    	    }
    	printf("%s
    ", flag ? "YES" : "NO");
        }
        return 0;
    }
    
  • 相关阅读:
    一般图的最大匹配-带花树算法
    Codeforces703D-Mishka and Interesting sum-离线树状数组
    HDU4578-代码一点都不长的线段树
    AOJ.综合训练.2016-12-1
    AOJ.综合训练.2016-12-1
    队列的实现
    队列的实现
    栈的实现
    栈的实现
    贪心算法总结
  • 原文地址:https://www.cnblogs.com/pangblog/p/3278464.html
Copyright © 2011-2022 走看看