A 计算 K 置位下标对应元素的和
模拟
class Solution {
public:int pop_cnt(int x) {//求x的二进制表示中的1的位数int res = 0;for (; x; x >>= 1)if (x & 1)res++;return res;}int sumIndicesWithKSetBits(vector<int> &nums, int k) {int res = 0;for (int i = 0; i < nums.size(); i++)if (pop_cnt(i) == k)res += nums[i];return res;}
};
B 让所有学生保持开心的分组方法数
排序+枚举:首先对数组排序,设选出的学生的元素集合为 { x i } \{x_i\} {xi},未选的学生的元素集合为 { y i } \{y_i\} {yi},则有 m a x { x i } < ∣ { x i } ∣ < m i n { y i } max\{ x_i \} < |\{x_i\}| < min\{ y_i \} max{xi}<∣{xi}∣<min{yi},所有若存在选择方案,则选出学生一定是数组的一个前缀,且未选的学生是数组剩余的后缀,枚举可能的前后缀划分情况。
class Solution {
public:int countWays(vector<int> &nums) {sort(nums.begin(), nums.end());int n = nums.size();int res = 0;if (nums[n - 1] < n)//后缀为空res++;if (nums[0] > 0)//前缀为空res++;for (int i = 0; i < n - 1; i++) {//枚举前缀nums[0,i]if (nums[i] < i + 1 && nums[i + 1] > i + 1)res++;}return res;}
};
C 最大合金数
二分:因为所有合金都需要由同一台机器制造,所有枚举各台机器,二分查找能制造的最大合金数
class Solution {
public:using ll = long long;int maxNumberOfAlloys(int n, int k, int budget, vector<vector<int>> &composition, vector<int> &stock, vector<int> &cost) {int res = 0;for (auto &li: composition) {int l = 0, r = 3e8;while (l < r) {int mid = (l + r + 1) / 2;ll tmp = 0;for (int i = 0; i < n && tmp <= budget; i++) {if (1LL * mid * li[i] - stock[i] > 0)tmp +=(1LL * mid * li[i] - stock[i]) * cost[i];}if (tmp <= budget)l = mid;elser = mid - 1;}res = max(res, l);}return res;}
};
D 完全子集的最大元素和
质因数分解+哈希: 一组数字中每对元素的乘积都是一个完全平方数当且仅当数组中每个元素的奇数次质因子的集合相同 (例: 12 = 2 2 × 3 1 12=2^2\times 3^1 12=22×31 , 75 = 3 1 × 5 2 75=3^1\times 5^2 75=31×52,它们的奇数次质因子的集合都为 { 3 } \{3\} {3}),枚举元素同时更新奇数次质因子的集合对应的元素和。
class Solution {
public:using ll = long long;long long maximumSum(vector<int> &nums) {map<vector<int>, ll> s;ll res = 0;for (int i = 1; i <= nums.size(); i++) {int cur = i;vector<int> t;//cur的奇数次质因子的集合for (int f = 2; f * f <= cur; f++)//质因数分解if (cur % f == 0) {int cnt_f = 0;while (cur % f == 0) {cur /= f;cnt_f++;}if (cnt_f & 1)//f是奇数次质因子t.push_back(f);}if (cur != 1) {t.push_back(cur);}s[t] += nums[i - 1];//更新元素和}res = 0;for (auto &[_, si]: s)res = max(res, si);return res;}
};