hdu5616 Jam's balance

来源:互联网 发布:淘宝解冻的金额在哪 编辑:程序博客网 时间:2024/05/18 02:21

Jam's balance

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


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
121 43245
 

Sample Output
NOYESYES
Hint
For the Case 1:Put the 4 weight aloneFor the Case 2:Put the 4 weight and 1 weight on both side

直接暴力枚举所有可能的值,然后查表就好了


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;int rec[25],dp[25][2005];int main(){    int T, n, m, sum;    scanf("%d", &T);    while (T--)    {        sum = 0;        scanf("%d", &n);        memset(dp, 0, sizeof(dp));        for (int i = 0; i < n; i++)        {            scanf("%d", &rec[i]);            sum += rec[i];            dp[i][0] = 1;        }        dp[0][rec[0]] = 1;        for (int i = 1; i < n; i++)        {            for (int j = 1; j <= sum; j++)            {                dp[i][j] |= dp[i - 1][j];                dp[i][j + rec[i]] |= dp[i - 1][j];                dp[i][abs(j - rec[i])] |= dp[i - 1][j];            }        }        scanf("%d", &m);        while (m--)        {            int weight;            scanf("%d", &weight);            if (dp[n - 1][weight])                puts("YES");            else                puts("NO");        }    }    return 0;}



1 0