传送门
此题的第一个版本,n较小,m较大。考虑dfs。复杂度O(2^n).
#include<bits/stdc++.h>
using namespace std;
double sum=0;
int a[20]={0},flag=0;
void dfs(int step, double num,int n)
{
if(flag==1)
return;
if(step==n)
{
if(num==sum/2)
flag=1;
return;
}
dfs(step+1,num+a[step],n);
dfs(step+1,num,n);
return;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
dfs(0,0,n);
if(flag==1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
flag=0;
sum=0;
}
return 0;
}
2580则是第二个版本,n较大,m较小。考虑dp。01背包问题。
01背包也有不同的写法。
#include <iostream>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <deque>
#include <cstdlib>
#define lowbit(x) ((x) & -(x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
const int maxn = 1e4 + 7;
const int INF = 0x3f3f3f3f;
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const double pi = acos(-1.0);
inline int read(){
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if (ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = (x<<1) + (x<<3) + (ch^48);
ch = getchar();
}
return x * f;
}
int a[maxn];
int dp[maxn];
int v[maxn];
int main(){
ios::sync_with_stdio(false);
// freopen("text.txt","r",stdin);
// freopen("out1.txt","w",stdout);
int n;
while(cin >> n){
int sum = 0;
memset(dp,0,sizeof(dp));
for (int i = 0; i < n; i ++)
cin >> a[i], sum += a[i];
if (sum & 1)
cout << "NO" << endl;
else{
for (int i = 0; i < n & !dp[sum/2]; i ++){
int cnt = 0;
for (int j = 0; j <= sum; j ++){
if (dp[j])
v[cnt ++] = j + a[i];
}
for (int j = 0; j < cnt; j ++)
dp[v[j]] = 1;
dp[a[i]] = 1;
}
cout << (dp[sum/2] ? "YES" : "NO") << endl;
}
}
return 0;
}
#include <iostream>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <deque>
#include <cstdlib>
#define lowbit(x) ((x) & -(x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
const int maxn = 1e4 + 7;
const int INF = 0x3f3f3f3f;
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const double pi = acos(-1.0);
inline int read(){
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if (ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = (x<<1) + (x<<3) + (ch^48);
ch = getchar();
}
return x * f;
}
int a[maxn];
bitset<maxn>dp;
int v[maxn];
int main(){
ios::sync_with_stdio(false);
// freopen("text.txt","r",stdin);
// freopen("out1.txt","w",stdout);
int n;
while(cin >> n){
int sum = 0;
dp.reset();
for (int i = 0; i < n; i ++)
cin >> a[i], sum += a[i];
if (sum & 1)
cout << "NO" << endl;
else{
dp[0] = 1;
for (int i = 0; i < n; i ++){
dp |= dp << a[i];
}
cout << (dp[sum/2] ? "YES" : "NO") << endl;
}
}
return 0;
}
这两种都不算特别正常的01背包,但是他们有自己的优势,1的优势空间复杂度小,2的优势:时间复杂度和空间复杂度都较小。
下面是01背包的较为标准的写法:
#include <iostream>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <deque>
#include <cstdlib>
#define lowbit(x) ((x) & -(x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
const int maxn = 1e4 + 7;
const int INF = 0x3f3f3f3f;
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const double pi = acos(-1.0);
inline int read(){
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if (ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = (x<<1) + (x<<3) + (ch^48);
ch = getchar();
}
return x * f;
}
int a[maxn];
int dp[102][maxn];
int v[maxn];
int main(){
//ios::sync_with_stdio(false);
// freopen("text.txt","r",stdin);
// freopen("out1.txt","w",stdout);
int n;
while(cin >> n){
int sum = 0;
for (int i = 1; i <= n; i ++)
a[i] = read(), sum += a[i];
if (sum & 1)
puts("NO");
else{
for (int i = 1; i <= n; i ++)
for (int j = 0; j <= sum/2; j ++)
dp[i][j] = 0;
for (int i = 1; i <= n && !dp[n][sum/2]; i ++){
dp[i][a[i]] = 1;
for (int j = 0; j <= sum/2; j ++){
if (i >= 1 && dp[i-1][j]){
dp[i][j] = 1;
dp[i][j+a[i]] = 1;
}
}
}
puts((dp[n][sum/2] ? "YES" : "NO"));
}
}
return 0;
}
还有一种方法:
#include <iostream>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <deque>
#include <cstdlib>
#define lowbit(x) ((x) & -(x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
const int maxn = 1e4 + 7;
const int INF = 0x3f3f3f3f;
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const double pi = acos(-1.0);
inline int read(){
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if (ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = (x<<1) + (x<<3) + (ch^48);
ch = getchar();
}
return x * f;
}
int a[maxn];
int dp[maxn];
int v[maxn];
int main(){
ios::sync_with_stdio(false);
// freopen("text.txt","r",stdin);
// freopen("out1.txt","w",stdout);
int n;
while(cin >> n){
int sum = 0;
for (int i = 1; i <= n; i ++)
cin >> a[i], sum += a[i];
if (sum & 1)
puts("NO");
else{
memset(dp,0,sizeof(dp));
dp[0] = 1;
for (int i = 1; i <= n; i ++){
for (int j = sum/2; j >= a[i]; j --){
dp[j] |= dp[j-a[i]];
}
}
puts((dp[sum/2] ? "YES" : "NO"));
}
}
return 0;
}