题解 | #分割等和子集#
分割等和子集
https://www.nowcoder.com/practice/65ade309fa4d4067a9add749721bfdc0
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
int total = 0;
vector<int> a(n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
total += a[i];
}
if (total % 2 != 0) {
puts("false");
return 0;
}
int mx = (1 << 31) - 2;
total /= 2;
vector<int> dp(total, mx);
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = total; j >= a[i]; j--) {
dp[j] = min(dp[j - a[i]] + 1, dp[j]);
}
}
if (dp[total] == mx) {
puts("false");
} else puts("true");
return 0;
}
算法常用解题技巧 文章被收录于专栏
算法常用解题技巧
