Jam's balance
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 109 Accepted Submission(s): 47
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.
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.
Output
You should output the "YES"or"NO".
Sample Input
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 side
题意:有n个砝码和一个没有游标的天平,砝码能放左边和右边。给出m个重量ki,问这些ki哪些是能测出来的,哪些是不能测出来的?
题解:妹妹的!!!无语啊,k没给范围,完了特判k>sum直接输出NO。内hack了。 母函数啦,前天才做的题。
链接: HDOJ 1709的题解 一模一样的题啦。
代码如下:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int c1[2010],c2[2010];
int a[22];
int main()
{
int t,n,m,i,j,k,sum,num;
scanf("%d",&t);
while(t--)
{
sum=0;
scanf("%d",&n);
for(i=1;i<=n;++i)
{
scanf("%d",&a[i]);
sum+=a[i];
}
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
c1[0]=c1[a[1]]=1;
num=a[1];
for(i=2;i<=n;++i)
{
for(j=0;j<=num;++j)
{
for(k=0;k<=a[i]&&k+j<=sum;k+=a[i])
{
c2[k+j]+=c1[j];
c2[abs(j-k)]+=c1[j];
}
}
num+=a[i];
for(j=0;j<=num;++j)
{
c1[j]=c2[j];
c2[j]=0;
}
}
scanf("%d",&m);
while(m--)
{
scanf("%d",&num);
if(num>sum)
{
printf("NO\n");
continue;
}
if(c1[num])
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}