HDU 5616 Jam's balance
2016-04-01 22:50
429 查看
Jam's balance
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 806 Accepted Submission(s): 386
[align=left]Problem Description[/align]
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
[align=left]Source[/align]
BestCoder Round #70
[align=left]Recommend[/align]
hujie | We have carefully selected several similar problems for you: 5654 5653 5652 5651 5650
刚开始用DFS做的,没做出来,后来就用了DP,由于两边都可以放,所以和物品放相同的一边时相当于减,所以把01背包再反向进行一次。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int dp[2100],wei[30],n; int main() { int t,i,num,j,m; scanf("%d",&t); while(t--) { memset(dp,0,sizeof(dp)); scanf("%d",&n); int sum=0; for(i=0;i<n;i++) { scanf("%d",&wei[i]); sum+=wei[i]; } dp[0]=1; for(i=0;i<n;i++) { for(j=sum;j>=wei[i];j--) { dp[j]=max(dp[j],dp[j-wei[i]]); } } for(i=0;i<n;i++) { for(j=0;j+wei[i]<=sum;j++) dp[j]=max(dp[j],dp[j+wei[i]]); } scanf("%d",&m); while(m--) { scanf("%d",&num); if(dp[num]) printf("YES\n"); else printf("NO\n"); } } return 0; }
相关文章推荐
- 二分搜索
- [BZOJ 1592] [Usaco2008 Feb] 路面修整
- Pollard's Rho Algorithm——求正整数的质因数
- JS没有重载(读书笔记)
- AndroidStudio使用札记
- spring的生命周期(参考)
- 书单
- YY项目之TabLayout自定义Tab的title
- Vim配置
- 《算法(第四版)》排序-----插入排序
- 成为Web开发人员的7个简单步骤
- Play Framework 2.5 sbt-fork-run-plugin;2.5.1: not found
- leetcode---Counting Bits
- [BZOJ 4439] [swerc 2015] Landscaping
- Windows平台分布式架构实践 - 负载均衡
- 算法实例-C#-快速排序-QuickSort
- JaveWeb中实现分页的总结
- 浅谈block(2)
- Java 1.7 ReentrantLock源码解析
- Java Web 通过CKEditor实现在线编译器