HDU5616 Jam's balance 动态规划

来源:互联网 发布:程序员的思维锻炼 编辑:程序博客网 时间:2024/05/16 11:55

Jam's balance

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


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 MM 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
 

题目大意:告诉你N个砝码,问能否测量出质量为M的物品。

分析:动态规划,对于前X个砝码,若能组合出质量为K的物品,则需要前X-1个砝码能组合出K-W[X]或K+W[X]或K的重量。

最后直接访问DP数组即可。

CODE:

//************************************************************************////*Author : Handsome How                                                 *////************************************************************************////#pragma comment(linker, "/STA    CK:1024000000,1024000000")#pragma warning(disable:4996) #include <vector>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#include <algorithm>#include <sstream>#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <cstring>#include <ctime>                                                #include <cassert>#if defined(_MSC_VER) || __cplusplus > 199711L#define aut(r,v) auto r = (v)#else#define aut(r,v) __typeof(v) r = (v)#endif#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)#define fur(i,a,b) for(int i=(a);i<=(b);i++)#define furr(i,a,b) for(int i=(a);i>=(b);i--)#define cl(a) memset((a),0,sizeof(a))using namespace std;typedef long long LL;//----------------------------------------------------bool dp[25][2500];int    a[30];int main(){    //freopen("E:\\data.in", "r", stdin);    ios :: sync_with_stdio(false);    int T;    scanf("%d", &T);    while (T--)    {        int n;        scanf("%d", &n);        fur(i, 1, n)scanf("%d", &a[i]);        cl(dp);        dp[0][0] = true;        fur(i, 1, n)fur(j, 0, 2000) {            if (j >= a[i])                if (dp[i - 1][j - a[i]])                    dp[i][j] = 1;            if (j + a[i] <= 2000)                if (dp[i - 1][j + a[i]])                    dp[i][j] = 1;            if (dp[i - 1][j])                dp[i][j] = 1;        }        int q;        scanf("%d", &q);        while (q--)        {            int t;            scanf("%d", &t);            if (t > 2000) {                printf("NO\n");                continue;            }            if (dp[n][t]) {                printf("YES\n");                continue;            }            printf("NO\n");        }    }    return 0;}


 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 西门子滚桶洗衣机门打不开怎么办 洗衣机离合器螺丝卸不动怎么办 门锁保险栓坏了怎么办 小车电瓶没电了怎么办 重装机兵战车底盘坏了怎么办 父亲沉迷安利十年该怎么办 脚的大脚骨痛怎么办 自考准考证号忘记了怎么办 有桌子老师不出马怎么办 电商遇到职业打假人怎么办 超市遇到职业打假人怎么办 阿里巴巴碰到职业打假人怎么办 商家遇到职业打假人怎么办 买过期食品不赔怎么办 淘宝卖假货遇到打假师怎么办 网店遇到职业打假人怎么办 职业打假师把我起诉法院怎么办 被职业打假举报了怎么办 车档杆拉不动显示不在p档怎么办 宜人贷还不起了怎么办 买高跟鞋一只脚合适一只脚挤怎么办 脚瘦穿高跟鞋撑不起来怎么办 银川市阅海幼儿园进不去怎么办 考编专业不对口怎么办 北京55中国际部怎么办 初中数学没学好高中怎么办 靴子大了一码怎么办 靴子买大了一码怎么办 马丁靴大了一码怎么办 社保掌上通登录密码忘记怎么办 录微课时忘词怎么办 微课掌上通看不到信息怎么办 五年级学生上课很吵新老师怎么办 跟财务老师吵起来怎么办 qq把微信冻结了怎么办 微信给封号了怎么办 微信久了没登录冻结了怎么办 换手机号了微店怎么办 ai文件置入后都是字怎么办 excel加载项被禁用了怎么办 被期刊网骗了怎么办