HDU 5616 Jam's balance(Jam的天平)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt }
h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: center; font-family: 宋体; color: rgb(26,92,200); font-weight: bold; font-size: 24.0000pt }
span.10 { font-family: "Times New Roman" }
span.15 { font-family: "Times New Roman"; color: rgb(26,92,200) }
span.16 { font-family: "Times New Roman"; color: rgb(26,92,200) }
p.pre { margin: 0pt; margin-bottom: .0001pt; text-align: left; font-family: 宋体; font-size: 12.0000pt }
span.msoIns { text-decoration: underline; color: blue }
span.msoDel { text-decoration: line-through; color: red }
table.MsoNormalTable { font-family: "Times New Roman"; font-size: 10.0000pt }
div.Section0 { }

HDU 5616 Jam‘s balanceJam的天平

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

 

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.

Jim 有一个天平和N块砝码。(1≤N≤20)
使用天平只能得知两端质量是否相等。
砝码可以放置在左右任意一端。
判断天平能否测量质量为M的物体。

CN

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 MM is the weight of the object being measured.

第一行有一个整数T(1≤T≤5),表示测试用例数。
对于每组测试用例:
第一行为N,表示砝码数。
第二行有N个数,第i个数wi(1≤wi≤100)表示
第三行有一个整数M。M为待测物体质量。

CN

Output - 输出

  You should output the "YES"or"NO".

输出"YES"or"NO"。

CN

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

例子1:一端放置4
例子2:放置4和1在同一端

CN

题解

  DP水题,先算+,再算-,没了。

代码 C++

 1 #include <cstdio>
 2 #include <cstring>
 3 #define MX 1500
 4 bool dp[MX];
 5 int main() {
 6     int t, n, i, j, data[25];
 7     scanf("%d", &t);
 8     while (t--) {
 9         scanf("%d", &n);
10         for (i = 0; i < n; ++i) scanf("%d", data + i);
11         memset(dp, 0, sizeof dp); dp[0] = 1;
12         for (i = 0; i < n; ++i) {
13             for (j = MX; ~j; --j) if (dp[j]) dp[j + data[i]] = 1;
14         }
15         for (i = 0; i < n; ++i) {
16             for (j = data[i]; j < MX; ++j) if (dp[j]) dp[j - data[i]] = 1;
17         }
18         scanf("%d", &n);
19         for (i = 0; i < n; ++i) {
20             scanf("%d", &j);
21             dp[j] ? puts("YES") : puts("NO");
22         }
23     }
24     return 0;
25 }

HDU 5616 Jam's balance(Jam的天平)

时间: 2024-10-07 02:32:10

HDU 5616 Jam's balance(Jam的天平)的相关文章

HDU 5616 Jam&#39;s balance 背包DP

Jam's balance 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 me

HDU 5616 Jam&#39;s balance(暴力枚举子集)

题目链接:点击打开链接 题意:有一个没有游标的天平,和n个秤砣,m个询问, 每次一个k,问可否秤出k这个重量. 秤砣可以放两边. 思路:因为n最大20, 暴力枚举子集. 因为可以放两边, 所以每次再跑一遍, 减去每个的重量, 将答案保存. 比赛的时候忘了限制边界,虽然过了终测数据, 却被人用大数据hack了(RE), 还是自己程序写的不够鲁棒, 思考的不完善. 细节参见代码: #include<cstdio> #include<cstring> #include<algori

HDU 5616 Jam&#39;s balance(DP)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1810    Accepted Submission(s): 754 Problem Description Jim has a balance an

Jam&#39;s balance HDU - 5616 (01背包基础题)

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. In

hdu 5616 Jam&#39;s balance(dp 正反01背包)

来自官方题解: AC代码: 1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<math.h> 7 #include<algorithm> 8 #include<queu

Jam&#39;s balance set 暴力

Jim has a balance and N weights. (1≤N≤20)(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 weig

杭电 HDU ACM 1709 The Balance

The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6304    Accepted Submission(s): 2592 Problem Description Now you are asked to measure a dose of medicine with a balance and a number

hdu 5616

Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 178    Accepted Submission(s): 77 Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(the

BestCoder Round #70

模拟 1001 Jam's math problem 判断b ^ 2 - 4ac是否为完全平方数.当delta < 0, sqrt (delta) 输出为nan, 但是好像也能计算? #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <string> #include <iostream> #include &