HDU5616--Jam‘s balance(二进制枚举)

Jam's balance

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


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

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

题意:有一个没有游标的天平,和n个秤砣,m个询问, 每次一个k,问可否秤出k这个重量。 秤砣可以放两边。

思路:我们可以二进制枚举每一个子集,还要注意一点就是,比如我我们有两个秤砣:2、3、15;我们不仅记录2 + 3 + 15还要考虑15 - 2 - 3,如果把负数考虑进来那就是,2^{40} 这时二进制枚举显然不能满足。所以我们只枚举一半数据,剩下一半就map查询就大大降低复杂度,大概20*2^{20}*5,能过但不是最优的解法,还是直接01背包比较直接。

不懂二进制枚举的请点击这里

折半枚举:

#include <iostream>
#include <map>
using namespace std;
int a[45];
map <int, bool> mp1, mp2;
string str;
int main()
{
    int t, n, q, sum = 0; 
	cin >> t;
    while(t--)
    {
		cin >> n;
		mp1.clear(); mp2.clear();
    	for(int i = 0; i < n; i++)
			cin >> a[i], a[n + i] = a[i];

		for(int i = 0; i < (1 << n); i++)
		{
			sum = 0;
			for(int j = 0; j < n; j++)
			{
				if(i & (1 << j))
					sum += a[j];
			}
			mp1[sum] = true;
		}
		for(int i = 0; i < (1 << n); i++)
		{
			sum = 0;
			for(int j = 0; j < n; j++)
			{
				if(i & (1 << j))
					sum += a[n + j];
			}
		    mp2[sum] = true;
		}
		int q, x, flag;
		cin >> q;
		for(int i = 0; i < q; i++)
		{
			cin >> x;
			flag = 0;
			for(auto it : mp1)
			{
				int tmp = x - it.first;
				if(tmp <= 0) tmp = -tmp;
				else continue;
				if(mp2.count(tmp))
				{
					puts("YES");
					flag = 1;
					break;
				}
			}
			if(!flag) puts("NO");
		}
	}
}

01背包:

#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;
 
int dp[4005];
int a[30];
int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n;
		cin >> n;
		memset(dp, 0, sizeof(dp));
		for(int i = 0; i < n; i++)
            cin >> a[i];
		dp[0] = 1;
		
		for(int i = 0; i < n; i++)
            for(int j = 2000; j >= a[i]; j--)
                dp[j] |= dp[j - a[i]];

        for(int i = 0; i < n; i++)
            for(int j = 0; j <= 2000; j++)
                dp[j] |= dp[j + a[i]];
		
		int q; cin >> q;
		while(q--)
		{
			int x; cin >> x;
            if(dp[x]) puts("YES");
            else puts("NO");
		}
	}
	return 0;
}

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值