不可分割的子集- Hackerrank

Non-Divisible Subset - Hackerrank

本文关键字:Hackerrank 子集 不可分割      更新时间:2023-10-16

我正试图解决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;
}