HDU 5616 Jam's balance(简单DP)
2017-08-17 17:12
295 查看
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
You should output the "YES"or"NO".
[align=left]Sample Input[/align]
1
2
1 4
3
2
4
5
[align=left]Sample Output[/align]
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 是否可以用已有砝码称出。
从第一个开始更新状态,OK[i][j] 表示考虑到第 i 个时 重量 j 是否可以被称出。1 ~ n 更新一遍即可。
代码:
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
You should output the "YES"or"NO".
[align=left]Sample Input[/align]
1
2
1 4
3
2
4
5
[align=left]Sample Output[/align]
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 是否可以用已有砝码称出。
从第一个开始更新状态,OK[i][j] 表示考虑到第 i 个时 重量 j 是否可以被称出。1 ~ n 更新一遍即可。
代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn1 = 25; const int maxn2 = 2 * 1e3 + 5; bool ok[maxn1][maxn2]; int a[maxn1]; int n; void init() { memset(ok,false,sizeof(ok)); ok[0][0] = ok[0][a[0]] = true; for(int i = 1;i < n; ++i) { for(int j = 0;j < maxn2; ++j) { if(!ok[i - 1][j]) continue; ok[i][j] = ok[i - 1][j]; ok[i][j + a[i]] = true; if(j > a[i]) ok[i][j - a[i]] = true; else ok[i][a[i] - j] = true; } } } int main() { int t,m,p; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i = 0;i < n; ++i) scanf("%d",&a[i]); init(); scanf("%d",&p); while(p--) { bool r = false; scanf("%d",&m); if(ok[n - 1][m]) printf("YES\n"); else printf("NO\n"); } } return 0; }
相关文章推荐
- hdu 5616 Jam's balance(简单dp)
- HDU 5616 Jam's balance(dp)
- HDU 5616 Jam's balance(dp)
- hdu 5616 Jam's balance
- HDU 5616 Jam's balance(01背包)
- hdu_5616_Jam's balance(暴力枚举子集||母函数)
- HDU 5616 Jam's balance(01背包)
- hdu 5616 Jam's balance(dp 正反01背包)
- 【折半枚举】HDU_5616_Jam's balance
- HDU 5616 Jam's balance
- HDU 5616 Jam's balance (乱搞思维)
- HDU 5616 Jam's balance dp, bitset
- HDU 5616 Jam's balance
- HDU 5616 Jam's balance
- 【HDU】5616 - Jam's balance(母函数变型)
- hdu 5616 Jam's balance ( 动态规划 )
- HDU 5616 Jam's balance 背包DP
- HDU 5616 Jam's balance (折半枚举or正反背包)
- HDU 5616 Jam's balance(暴力枚举子集)
- HDU 5616 Jam's balance(01背包)