您的位置:首页 > 其它

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

2016-08-03 16:10 441 查看

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: