hdoj Jam's balance 5616 (母函数&暴力)
2016-02-29 08:32
387 查看
Jam's balance
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 690 Accepted Submission(s): 328
[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问题描述
Jam有NN个砝码和一个没有游标的天平,现在给他(1 \leq N \leq 20)(1≤N≤20)个砝码,砝码可以放左边,也可以放右边,问可不可以测出所问的重量, 问的个数为(1 \leq M \leq 100)(1≤M≤100)个.输入描述
第一行T(1 \leq T \leq 5)T(1≤T≤5),表示TT组数据。 接下来TT组数据: 接下来第一行一个数NN,表示砝码个数。 接下来第二行NN个数,表示砝码们的重量(1 \leq w_i \leq 100)(1≤wi≤100)。 接下来第三行一个数MM,表示询问个数。 接下来MM行每行一个数kk,表示一个询问。输出描述
对于每组数据,输出"YES"或者"NO"输入样例
1 2 1 4 3 2 4 5输出样例
NO YES YESHint
单独放4,可以测出重量为4的 在同一边放4,1,可以测出重量为5的
#include<stdio.h> #include<string.h> #include<algorithm> #define INF 0x3f3f3f3f #define ll long long #define N 2010 using namespace std; int a[21]; int c1 ; int c2 ; int main() { int t,n,m,i,j,k; scanf("%d",&t); while(t--) { int sum=0; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } memset(c1,0,sizeof(c1)); memset(c2,0,sizeof(c2)); c1[a[0]]=c1[0]=1; for(i=2;i<=n;i++) { for(j=0;j<=sum;j++) { for(k=0;k<=a[i-1]&&k+j<=sum;k+=a[i-1]) { c2[k+j]+=c1[j];//相加 c2[abs(k-j)]+=c1[j];//相减 } } for(j=0;j<=sum;j++) { c1[j]=c2[j]; c2[j]=0; } } scanf("%d",&m); while(m--) { int kk; scanf("%d",&kk); printf(c1[kk]?"YES\n":"NO\n"); } } return 0; }//暴力
#include<stdio.h> #include<string.h> #include<algorithm> #define INF 0x3f3f3f3f #define ll long long #define N 2010 using namespace std; int vis ; int s ; int main() { int t,n,m,i,j,k; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(vis,0,sizeof(vis)); memset(s,0,sizeof(s)); int mm=0; vis[0]=0; for(i=0;i<n;i++) { int x; scanf("%d",&x); s[x]=1; for(j=0;j<=mm;j++) { if(vis[j]) { s[j+x]=1; s[abs(j-x)]=1; } } mm+=x; for(j=0;j<=mm;j++) vis[j]=s[j]; } scanf("%d",&m); while(m--) { scanf("%d",&k); printf(vis[k]?"YES\n":"NO\n"); } } return 0; }
相关文章推荐
- (转)Java垃圾回收机制
- 242. Valid Anagram
- 238. Product of Array Except Self
- UVa 699 The Falling Leaves 树的深度优先搜索
- 242. Valid Anagram
- UVA11387 - The 3-Regular Graph(推理)
- 去它的h5,我还是用js写原生跨平台app吧
- 网络基础知识(一):网络分层、UDP协议
- 238. Product of Array Except Self
- android性能优化
- Python图像处理库PIL的ImageGrab模块介绍
- 写在最前面 - 每天5分钟玩转 OpenStack(1)
- SpringMVC源码解析 - HandlerAdapter - @SessionAttributes注解处理
- Android Studio sdk tools文件夹下文件缺失问题以及解决方法
- MATLAB数据类型
- 类和对象
- git使用流程图
- SQL高级优化之经常使用的优化策略-2(The Return Of The King)
- python datetime
- 【积水成渊-逐步定制自己的Emacs神器】2:基本的定制Emacs