如何查找子序列中等于总和的最大子集的大小

How to find size of largest subset of a sub-sequence equal to a sum

本文关键字:和的 于总 子集 何查找 查找      更新时间:2023-10-16

我有这个问题来自黑客地球

给定一个由 N 个整数、C 卡和 S 和 S 和组成的数组。每张卡都可以使用 将给定数组中的整数递增或递减 1。 查找是否有任何子集(在使用任何编号的卡之后/之前)与 给定数组中的 S 总和。

输入格式

输入的第一行包含一个整数 T,表示编号。 测试用例。每个测试用例有 2 行输入。每个的第一行 测试用例有三个整数N(数组的大小),S(子集和)和 C(卡的编号)。每个测试用例的第二行有 N 个整数 数组(a1 到 aN)由空格分隔。

约束

1<=T<=100
1<=N<=100
1<=S<=10000
0<=C<=100
1<=ai<=100

输出格式

如果存在具有给定总和的子集,则打印 TRUE,否则打印 FALSE。

所以这基本上是子集和问题的变体,但不是找出具有总和S的给定子集是否存在,我们需要找到从序列indexN-1的最大子集,其值为s,并将其长度与我们的C值进行比较,看看它是否更大。如果是,那么我们有足够的元素来使用我们的C卡修改总和,然后我们打印出我们的答案。这是我的代码

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int N, S, C;

int checkSum(int index, int s, vector<int>& a, vector< vector<int> >& dP) {
if (dP[index][s] != -1)
return dP[index][s];
int maxNums = 0;    // size of maximum subset array
for (int i = index; i < N; i++) {
int newSum = s - a[i];
int l = 0;
if (newSum == 0) {
l = 1;
} if (newSum > 0) {
if (i < (N-1)) {    // only if we can still fill up sum
l = checkSum(i + 1, newSum, a, dP);
if (l > 0)      // if it is possible to create this sum
l++;        // include l in it
} else {
// l stays at 0 for there is no subset that can create this sum
}
} else {
// there is no way to create this sum, including this number, so skip it;
if (i == (N-1))
break;      // don't go to the next level
// and l stays at 0
}
if (l > maxNums) {
maxNums = l;
}
}
dP[index][s] = maxNums;
return maxNums;
}

int main() {
int t;
cin >> t;
while (t--) {
cin >> N >> S >> C;
vector<int> a(N);
for (int i = 0; i < N; i++)
cin >> a[i];
vector< vector<int> > dP(N, vector<int>(S + C + 2, -1));
bool possible = false;
for (int i = 0; i <= C; i++) {
int l = checkSum(0, S-i, a, dP);
int m = checkSum(0, S+i, a, dP);
if ( (l > 0 && l >= i) || (m > 0 && m >= i) ) {
cout << "TRUE" << endl;
possible = true;
break;
}
}
if (!possible)
cout << "FALSE" << endl;
}
return 0;
}

所以基本上,0 表示不可能从元素索引到 N-1 创建一个等于 s 的子集,而 -1 表示我们还没有计算它。任何其他值表示总和为 s 的最大子集的大小。此代码未通过所有测试用例。怎么了?

您错过了以下行中的else

} if (newSum > 0) {

在某些情况下,这会使您的程序在按l更新maxNums之前有一个意外的提前休息。

例如,N=1、S=5、C=0、a={5}


潜在的逻辑问题

您已将要使用的卡号限制为不超过子集大小,而问题从未说明您不能将多张卡应用于相同的整数。

我的意思是l >= im >= i

if ( (l > 0 && l >= i) || (m > 0 && m >= i) ) {

看来你有逻辑缺陷。

您需要找到最短的子集(总和在范围S-C..S+C)并将其大小与C进行比较。如果子集较短,则可以进行所需的总和。