HDU 5616 Jam's balance(简单DP)
来源:互联网 发布:阿里云2m带宽 编辑:程序博客网 时间:2024/06/06 00:02
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.
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 integerT(1≤T≤5) , means T test cases.
For each test case :
The first line isN , means the number of weights.
The second line areN number, i'th number wi(1≤wi≤100) means the i'th weight's weight is wi .
The third line is a numberM .M is the weight of the object being measured.
For each test case :
The first line is
The second line are
The third line is a number
Output
You should output the "YES"or"NO".
Sample Input
121 43245
Sample Output
NOYESYESHintFor the Case 1:Put the 4 weight aloneFor 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;}
阅读全文
0 0
- hdu 5616 Jam's balance(简单dp)
- HDU 5616 Jam's balance(简单DP)
- HDU 5616 Jam's balance(dp)
- HDU 5616 Jam's balance(dp)
- 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 5616Jam's balance
- 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(母函数变型)
- HDU 5616 Jam's balance (乱搞思维)
- hdu 5616 Jam's balance ( 动态规划 )
- HDU 5616 Jam's balance(01背包)
- 处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC"
- 【腾讯TMQ】这样的测试过程管理让你事半功倍
- 面试题:最常见最有可能考到的C语言面试题汇总
- 22
- Collection常见问题
- HDU 5616 Jam's balance(简单DP)
- Delphi 访问并打开局域网共享文件夹
- Oracle常用计算函数-yellowcong
- Ubuntu16.04 安装sublime text 3、java、IntelliJ IDEA、Tomcat
- pip install出错 not a trusted or secure host解决方案
- 关于HTML5那些事儿1
- Java四种线程池的使用
- 怎样设置Qt生成的应用程序图标
- iOS 手势识别(点按、长按、轻扫)