Jam's balance
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 188 Accepted Submission(s): 82
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
w
i
(1≤w
i
≤100)
means the i'th weight's weight is
w
i![]()
.
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
The second line are N
The third line is a number M
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
Source
解题思路:
这道题和杭电的1709是一样的,就是
砝码可以放在天枰的左右两端。
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
int c1[30000],c2[30000];
int a[30000];
bool cc[30000];
int main()
{
int i,j,n,sum;
int nn;
scanf("%d",&nn);
while(nn--)
{
scanf("%d",&n);
sum=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
for(i=0;i<=sum;i++)
{
c1[i]=0;
c2[i]=0;
}
c1[0]=1;
for(i=1;i<=n;i++)
{
for(j=0;j+a[i]<=sum;j++)
{
if(c1[j]==1)
{
c2[j]=1;
c2[j+a[i]]=1;
c2[abs(j-a[i])]=1;
}
}
for(j=0;j<=sum;j++)
{
c1[j]=c2[j];
c2[j]=0;
}
}
int num=0;
memset(cc,false,sizeof(cc));
for(i=1;i<=sum;i++)
{
if(c1[i]==0)
{
cc[i]=true;
}
}
int wnm;
scanf("%d",&wnm);
while(wnm--)
{
int yyy;
scanf("%d",&yyy);
if(yyy>sum)
{
printf("NO\n");
continue;
}
if(cc[yyy]==true)
{
printf("NO\n");
}
else
{
printf("YES\n");
}
}
}
return 0;
}