HDU 5616 Jam's balance (乱搞思维)

Jam’s balance

http://acm.hdu.edu.cn/showproblem.php?pid=5616

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


题意:
给一些砝码,和一个物品的重量。假设物品开始放在左盘,然后任取一些砝码任意放在哪个盘都可,问是否能使得天平平衡。

解题思路:
用一个数组保存砝码的重量, 同时在这个数组里面加上每个砝码的相反数。最后的问题即求 在这个数组中任取一些数看是否他们的和为这个物品的质量。如果取了一个砝码,则相当于把这个砝码放在了右盘,若取了一个砝码的相反数,则相当于把砝码放在了左边。如果两个数都取了,则相当于这个砝码没取。然后暴力一遍看那些重量是可以称出来的,直接O(1)查询即可。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

const int N = 110;
vector<int> vet;
bool is[2010];
int arr[N*2];
bool comp(const int &a, const int &b)
{
        return a > b;
}
int main()
{
        int T;
        scanf("%d",&T);
        while(T--)
        {
                memset(is,false,sizeof(is));
                vet.clear();
                int n, t = 0;
                scanf("%d",&n);
                for(int i=1;i<=n;i++) scanf("%d",&arr[++t]), arr[++t] = -arr[t-1];
                sort(arr+1,arr+1+t,comp);
                is[0] = true;
                vet.push_back(0);
                for(int i=1;i<=t;i++)
                {
                        int size  = vet.size();
                        for(int j = 0;j<size;j++)
                        {
                                if( arr[i] + vet[j] >=0 &&  !is[arr[i] + vet[j]])
                                {
                                        vet.push_back(arr[i] + vet[j]);
                                        is[arr[i] + vet[j]] = true;
                                }
                        }
                }
                int m;
                scanf("%d",&m);
                while(m--)
                {
                        int x;
                        scanf("%d",&x);
                        puts(is[x] ? "YES":"NO");
                }
        }
        return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值