10763 - Foreign Exchange (UVA)

题目链接如下:

Online Judge

这道题网上很多题解都是错的……(当然我的也不保证一定就对..)

代码如下:

#include 
#include 
#include 
// #define debug

struct stu{
    int original, target;
};
int n, i;
std::vector a, b;

bool cmp1(const stu &u, const stu &v){
    return u.original != v.original ? u.original < v.original : u.target < v.target;
}

bool cmp2(const stu &u, const stu &v){
    return u.target != v.target ? u.target < v.target : u.original < v.original;
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (scanf("%d", &n) == 1 && n){
        a.resize(n);
        b.resize(n);
        for (i = 0; i < n; ++i){
            scanf("%d %d", &a[i].original, &a[i].target);
            b[i].original = a[i].original;
            b[i].target = a[i].target;
        }
        sort(a.begin(), a.end(), cmp1);
        sort(b.begin(), b.end(), cmp2);
        i = 0;
        for (i = 0; i < n; ++i){
            if(a[i].original != b[i].target || a[i].target != b[i].original){
                break;
            }
        }
        printf("%s\n", i == n ? "YES" : "NO");
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

你可能感兴趣的:(UVA,c++)