不可分割的子集- Hackerrank
Non-Divisible Subset - Hackerrank
我正试图解决Hackerrank (https://www.hackerrank.com/challenges/non-divisible-subset)的不可分割子集问题。我试着用这个概念如果a和b的和能被k整除,那么a%k+b%k = k,然而,它不是很有效。以下是我目前为止写的内容:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
int k;
cin >> n;
cin >> k;
int j;
vector<int>numbers;
vector<int>remainders;
for(int i = 0; i < n; i++) {
int z;
cin >> z;
numbers.push_back(z);
}
for (vector<int>::iterator it = numbers.begin(); it != numbers.end(); it++) {
j = *it % k;
remainders.push_back(j);
}
for(vector<int>::iterator it2 = remainders.begin(); it2 != remainders.end(); it2++) {
int remainderCount = 0;
int otherRemainderCount = 0;
otherRemainderCount = std::count(remainders.begin(), remainders.end(), k-*it2);
remainderCount = std::count(remainders.begin(), remainders.end(), *it2);
if (remainderCount > otherRemainderCount) {
theChosenOne = *it2;
} else if (otherRemainderCount > remainderCount) {
theChosenOne = k-*it2;
}
cout << theChosenOne << endl;
}
return 0;
}
我为余数创建了一个向量,我使用std::cout函数来找出哪个余数在向量中出现得更多。如果K = 5, *it2 = 4, K -*it2 = 1。如果*it2出现的次数更多,那么我会选择*it2。否则,我会选择k-*it2
你的解决方案看起来是正确的,但是还需要做一些修改。
你基本上需要将数组中的数字散列到合适的位置。
将数组rem[k]
初始化为0。
遍历数组中的n
数字,并执行以下操作:
rem[array[i]%k]++;
现在你只需要处理rem[]
数组,以找到最大子集。rem
数组的最大大小为k<=100
。利用rem[]
阵列的小尺寸,有效地找到解决方案。
Edit:为您添加代码。
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n,i,k;
cin>>n>>k;
int arr[n];
int rem[k]={0};
for(i=0;i<n;i++)
{
cin>>arr[i];
}
for(i=0;i<n;i++)
{
rem[arr[i]%k]++;
}
int count = 0;
for(i=1;i<=k/2;i++)
{
count = count + max(rem[i],rem[k-i]);
}
count = count + (rem[0]>0?1:0);
if(k%2==0)
{
count = count - rem[k/2];
if(rem[k/2]>0)
count = count + 1;
}
cout<<count;
return 0;
}
找到rem[]
数组的内容后,是时候找到最大子集了。如果你选择rem[1]
,那么你不能选择rem[k-1]
,因为任何两个数字,一个来自rem[1]
,另一个来自rem[k-1]
,可以相加,这将被我们不想要的k
整除。所以我们找出rem[i]
和rem[k-i]
中最大的那个并把它加到计数
我的代码使用了上面的逻辑…
希望有帮助!!
int main() {
int n,k;
cin>>n>>k;
vector <int> a(n);
vector <int> r(k,0);
for(int i=0;i<n;i++)
{
cin>>a[i];
r[a[i]%k]++;
}
int ctr=min(1,r[0]);
for(int a=1;a<(k/2+1);a++)
{
if(a!=k-a)
ctr+=max(r[a],r[k-a]);
}
if(k%2==0&&r[k/2]!=0)
ctr++;
cout<<ctr;
return 0;
}
这似乎行得通
#include <stdio.h>
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int main() {
int n, k, a, total = 0;
scanf("%d %d", &n, &k);
int mods[k];
for (int i = 0; i < k; i++)
mods[i] = 0;
while (n--) {
scanf("%d", &a);
mods[a % k]++;
}
// can only have 1 value congruent to 0 mod k
total += min(1, mods[0]);
// if even, can only have 1 value congruent to k/2 mod k
if (k % 2 == 0)
total += min(1, mods[k / 2]);
// for all others, pick max of those k and n-k mod k
for (int d = 1; d < (k + 1) / 2; d++) { // for all others,
total += max(mods[d], mods[k - d]);
}
printf("%d", total);
return 0;
}
相关文章:
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 在子集化后将包含索引号的列表列表映射到标准索引序列
- 显示字符串的集合和子集
- 用于子集字符串的 Rcpp 函数
- 请发现它在 dev 中工作的错误,但在 hackerrank 中不起作用
- 为什么我的子集和方法不正确?
- 计算总和为 x 的所有整数子集(包括负数)
- 在尝试使用递归查找集合子集的总数时,我遇到了分割错误
- 从小于或等于某个 N 的数字列表中最小化或找到 n 个理想的子集和
- 使用 Rcpp 的高效矩阵子集
- 返回给定 SEXP 的子集,而无需知道实际的内部数据类型
- C++中的Hackerrank楼梯
- 使用递归从子集和中查找最大和
- 如何获得比较两个向量对的子集
- 实现伪多项式DP子集和
- 现代编译器会优化只引用对象子集的局部变量吗
- 类方法子集的惰性评估
- 子集相关位操作
- C++ 特征非连续子集
- 不可分割的子集- Hackerrank