大厂笔试题
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.7 KiB

  1. /*
  2. A包含B(A|B)= A, |B中的所有为1的二进制位A中都为1n个正整数Ai使Ai()
  3. nn个整数i个整数表示Ai0 Ai< 262144, 1 n2x1e5
  4. */
  5. #include <iostream>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <unordered_set>
  9. using namespace std;
  10. int main() {
  11. int n;
  12. cin >> n;
  13. vector<int> a(n);
  14. unordered_set<int> uniqueSet;
  15. // Read input and add to the set to remove duplicates
  16. for (int i = 0; i < n; ++i) {
  17. cin >> a[i];
  18. uniqueSet.insert(a[i]);
  19. }
  20. // Convert set back to vector
  21. vector<int> uniqueNumbers(uniqueSet.begin(), uniqueSet.end());
  22. // Sort in descending order to apply the greedy strategy
  23. sort(uniqueNumbers.rbegin(), uniqueNumbers.rend());
  24. int selectedCount = 0;
  25. vector<bool> covered(uniqueNumbers.size(), false);
  26. // For each number, see if it is necessary to cover the others
  27. for (size_t i = 0; i < uniqueNumbers.size(); ++i) {
  28. if (!covered[i]) {
  29. selectedCount++;
  30. for (size_t j = i + 1; j < uniqueNumbers.size(); ++j) {
  31. if ((uniqueNumbers[i] | uniqueNumbers[j]) == uniqueNumbers[i]) {
  32. covered[j] = true;
  33. }
  34. }
  35. }
  36. }
  37. cout << selectedCount << endl;
  38. return 0;
  39. }