hdu 5616 Jam'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<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 using namespace std;
15 #define ll long long
16 #define eps 1e-10
17 #define MOD 1000000007
18 #define N 26
19 #define M 2006
20 #define inf 1e12
21 int n,a[N];
22 int dp[M];
23 int main()
24 {
25     int t;
26     scanf("%d",&t);
27     while(t--){
28         memset(dp,0,sizeof(dp));
29         scanf("%d",&n);
30         int sum=0;
31         for(int i=0;i<n;i++){
32             scanf("%d",&a[i]);
33             sum+=a[i];
34         }
35         dp[0]=1;
36         for(int i=0;i<n;i++){
37             for(int j=sum;j>=a[i];j--){
38                 dp[j]|=dp[j-a[i]];
39             }
40         }
41
42         for(int i=0;i<n;i++){
43             for(int j=0;j+a[i]<=sum;j++){
44                 dp[j]|=dp[j+a[i]];
45             }
46         }
47
48         int m;
49         scanf("%d",&m);
50         while(m--){
51             int weight;
52             scanf("%d",&weight);
53             if(dp[weight]){
54                 printf("YES\n");
55             }else{
56                 printf("NO\n");
57             }
58         }
59
60     }
61     return 0;
62 }

hdu 5616 Jam's balance(dp 正反01背包)

时间: 2024-10-16 18:50:24

hdu 5616 Jam's balance(dp 正反01背包)的相关文章

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

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&#160;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; fo

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

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

HDU 5617 Jam&#39;s maze dp+滚动数组

题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5617 bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=666&pid=1003 题解: 设dp[x1][x2][i]表示第i步时,从(1,1)点走到了(x1,y1),(n,n)点走到了(x2,y2)点的合法的总数. 1 #include<iostream> 2 #include

HDU 5887 Herbs Gathering(搜索求01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=5887 题意: 容量很大的01背包. 思路: 因为这道题目背包容量比较大,所以用dp是行不通的.所以得用搜索来做,但是需要一些剪枝,先按体积排序,优先考虑体积大的物品,这样剪枝会剪得多一些(当然按照性价比排序也是可以的). 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdi

CSU 1547 Rectangle(dp、01背包)

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1547 Description Now ,there are some rectangles. The area of these rectangles is 1* x or 2 * x ,and now you need find a big enough rectangle( 2 * m) so that you can put all rectangles into it(th

poj 2184 Cow Exhibition(dp之01背包变形)

Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to the public that they are both smart and fun. In order to do this, Bessie has organized an exhibitio

HDU 1203 I NEED A OFFER!【01背包】

大意:01背包 分析:01背包 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 10005; 7 const int INF = 1000000000; 8 9 double dp[maxn]; 10 int cost[maxn]; 11 double prob[maxn]; 12 int main