CSU1323: ZZY and his little friends

Description

zzy养了一只小怪兽和N只凹凸曼,单挑的话每只凹凸曼都不是小怪兽的对手,所以必须由两只凹凸曼合作来和小怪兽战斗。凹凸曼A和凹凸曼B合作的战斗力为他们战斗力的异或值。现在由zzy从N只凹凸曼中选出两只来和小怪兽战斗。请问zzy能否选出两只凹凸曼使他们能够战胜小怪兽(他们的战斗力比小怪兽大)。

Input

输入有多个例子,直到文件结束。 每个例子的第一行含两个数N和M,表示有N(2<=N<=100000)只凹凸曼,小怪兽的战斗力为M(0<M<=1000000000)。接着有一行N个数,每个数Ai(0<Ai<M)表示每只凹凸曼的战斗力。

Output
对于每个例子输出一行,如果能选出两只凹凸曼使他们战胜小怪兽输出”YES”, 否则输出”NO”(不含引号)

Sample Input
2 5 1 1 2 6 5 2
Sample Output
NO YES

1. 法1:暴力(排序后去重)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){

    int m, n;
    while (cin >> n >> m){
        vector<int> nums(n);
        int cnt = 0;
        for (int i = 0; i < n; ++i){
            int t;
            cin >> t;
            nums[i] = t;
        }
        sort(nums.begin(), nums.end());
        int sz = unique(nums.begin(), nums.end()) - nums.begin();
                        // unique 操作必须经过排序,因其只能去除,相邻重复的元素
        for (int i = 0; i < sz; ++i){
            for (int j = i + 1; j < sz; ++j){
                if ((nums[i] ^ nums[j]) > m)
                    ++cnt;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/mtcnn/p/9423632.html