HDU 5616 Jam's balance

Jam's balance

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 806    Accepted Submission(s): 386


Problem Description
Jim has a balance and N weights. (1N20)
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(1T5) , 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(1wi100) 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 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
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5654  5653  5652  5651  5650 
刚开始用DFS做的,没做出来,后来就用了DP,由于两边都可以放,所以和物品放相同的一边时相当于减,所以把01背包再反向进行一次。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[2100],wei[30],n;
int main()
{
	int t,i,num,j,m;
	scanf("%d",&t);
	while(t--)
	{
		memset(dp,0,sizeof(dp));
		scanf("%d",&n);
		int sum=0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&wei[i]);
			sum+=wei[i];
		}
		dp[0]=1;
		for(i=0;i<n;i++)
		{
			for(j=sum;j>=wei[i];j--)
			{
				dp[j]=max(dp[j],dp[j-wei[i]]);
			}
		}
		for(i=0;i<n;i++)
		{
			for(j=0;j+wei[i]<=sum;j++)
		    dp[j]=max(dp[j],dp[j+wei[i]]);
		}
		scanf("%d",&m);
		while(m--)
		{
			scanf("%d",&num);
			if(dp[num])
			printf("YES\n");
			else
			printf("NO\n");
		}	
	}
	return 0;
}



 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值