Jam's balanceTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2403 Accepted Submission(s): 981 Problem Description Jim has a balance and N weights. (1≤N≤20) Input The first line is a integer T(1≤T≤5) , means T test cases. Output You should output the "YES"or"NO". Sample Input 1 2 1 4
扫描二维码关注公众号,回复:
3736521 查看本文章
3 2 4 5 Sample Output NO YES YES Hint For the Case 1:Put the 4 weight alone For the Case 2:Put the 4 weight and 1 weight on both sideSource Recommend hujie | We have carefully selected several similar problems for you: 6447 6446 6445 6444 6443 |
题解:枚举 0 ~ 2^n 次方,一个数可取可不取,共 2^n 种状态。
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#include<set>
#include<map>
#include<math.h>
#include<stack>
#include<vector>
#include<bitset>
#include<iostream>
#define ullmax 1844674407370955161
#define llmax 9223372036854775807
#define llmaxx 922337203685477580
#define intmax 2147483647
#define intmaxx 214748364
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
using namespace std;
int v[2005];
int main(){
int t,n,a[25],m,k;
scanf("%d",&t);
while(t--){
memset(v,0,sizeof(v));
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<(1<<n);i++){ // 2^n 种状态
int ans=0;
for(int j=0;j<n;j++){ // 总共有 n 个数位
if(i&(1<<j)){ // 判断每个数位是否为 1
ans+=a[j];
v[ans]=1;
}
}
for(int j=0;j<n;j++){ // 比如 : 2,7 两个砝码,可 表示状态有 : 2,7,9,5
if(ans-a[j]>=0)
v[ans-a[j]]=1;
}
}
scanf("%d",&m);
while(m--){
scanf("%d",&k);
if(v[k])
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}