HDOJ 5616 Jam's balance (暴力)

Jam's balance

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


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
 
题意:给你n个砝码,然后给你重量m,查看是否能称量出来

思路:暴力标记能够称量出来的重量,题解说是DP,但是因为数据小,所以说是可以暴力的。。


ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
int vis[MAXN];
int sum[MAXN];
int main()
{
    int t,n,m,a,i,j;
    scanf("%d",&t);
    while(t--)
    {
    	scanf("%d",&n);
    	ll cnt=0;
    	mem(vis);mem(sum);
    	int M=0;
    	for(i=0;i<n;i++)
    	{
    		scanf("%d",&a);
    		for(j=1;j<=M;j++)
    		{
    			if(vis[j])
    			{
    				sum[a+j]=1;
    				sum[abs(a-j)]=1;
				}
			}
			sum[a]=1;
			M+=a;
			for(j=1;j<=M;j++)
			vis[j]=sum[j];
		}
		scanf("%d",&m);
		while(m--)
		{
			scanf("%d",&a);
			if(a>=1&&a<=2000&&vis[a])
			printf("YES\n");
			else
			printf("NO\n");
		}
    }
    return 0;
}


你可能感兴趣的:(HDOJ 5616 Jam's balance (暴力))