Jam's balance
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 690 Accepted Submission(s): 328
Problem Description
Jim has a balance and N weights.
(1≤N≤20)
The balance can only tell whether things on different side are the same weight.
Weights can be put on left side or right side arbitrarily.
Please tell whether the balance can measure an object of weight M.
The balance can only tell whether things on different side are the same weight.
Weights can be put on left side or right side arbitrarily.
Please tell whether the balance can measure an object of weight M.
Input
The first line is a integer
T(1≤T≤5)
, means T test cases.
For each test case :
The first line is N , means the number of weights.
The second line are N number, i'th number wi(1≤wi≤100) means the i'th weight's weight is wi .
The third line is a number M . M is the weight of the object being measured.
For each test case :
The first line is N , means the number of weights.
The second line are N number, i'th number wi(1≤wi≤100) means the i'th weight's weight is wi .
The third line is a number M . M is the weight of the object being measured.
Output
You should output the "YES"or"NO".
Sample Input
1 2 1 4 3 2 4 5
Sample Output
NO YES YESHintFor the Case 1:Put the 4 weight alone For the Case 2:Put the 4 weight and 1 weight on both side问题描述Jam有N个砝码和一个没有游标的天平,现在给他(1≤N≤20)个砝码,砝码可以放左边,也可以放右边,问可不可以测出所问的重量, 问的个数为(1≤M≤100)个.输入描述第一行T(1≤T≤5),表示T组数据。 接下来T组数据: 接下来第一行一个数N,表示砝码个数。 接下来第二行N个数,表示砝码们的重量(1≤wi≤100)。 接下来第三行一个数M,表示询问个数。 接下来M行每行一个数k,表示一个询问。输出描述对于每组数据,输出"YES"或者"NO"输入样例1 2 1 4 3 2 4 5输出样例NO YES YESHint单独放4,可以测出重量为4的 在同一边放4,1,可以测出重量为5的#include<stdio.h> #include<string.h> #include<algorithm> #define INF 0x3f3f3f3f #define ll long long #define N 2010 using namespace std; int a[21]; int c1[N]; int c2[N]; int main() { int t,n,m,i,j,k; scanf("%d",&t); while(t--) { int sum=0; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } memset(c1,0,sizeof(c1)); memset(c2,0,sizeof(c2)); c1[a[0]]=c1[0]=1; for(i=2;i<=n;i++) { for(j=0;j<=sum;j++) { for(k=0;k<=a[i-1]&&k+j<=sum;k+=a[i-1]) { c2[k+j]+=c1[j];//相加 c2[abs(k-j)]+=c1[j];//相减 } } for(j=0;j<=sum;j++) { c1[j]=c2[j]; c2[j]=0; } } scanf("%d",&m); while(m--) { int kk; scanf("%d",&kk); printf(c1[kk]?"YES\n":"NO\n"); } } return 0; }
//暴力#include<stdio.h> #include<string.h> #include<algorithm> #define INF 0x3f3f3f3f #define ll long long #define N 2010 using namespace std; int vis[N]; int s[N]; int main() { int t,n,m,i,j,k; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(vis,0,sizeof(vis)); memset(s,0,sizeof(s)); int mm=0; vis[0]=0; for(i=0;i<n;i++) { int x; scanf("%d",&x); s[x]=1; for(j=0;j<=mm;j++) { if(vis[j]) { s[j+x]=1; s[abs(j-x)]=1; } } mm+=x; for(j=0;j<=mm;j++) vis[j]=s[j]; } scanf("%d",&m); while(m--) { scanf("%d",&k); printf(vis[k]?"YES\n":"NO\n"); } } return 0; }