hdu 5616
Jam's balance
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 370 Accepted Submission(s): 173
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.
, 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.
2
1 4
3
2
4
5
YES
YES
For the Case 1:Put the 4 weight alone
For the Case 2:Put the 4 weight and 1 weight on both side
#include<bits/stdc++.h>
using namespace std;
int t;
int n;
int fa[25];
map<int,int>dp1;
map<int,int>dp2;
int sum;
int m,exm;
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int i=1; i<=t; i++)
{
sum=0;
dp1.clear();
dp2.clear();
scanf("%d",&n);
for(int j=0; j<n; j++)
{
scanf("%d",&fa[j]);
sum+=fa[j];
}
dp1[0]=1;
dp1[fa[0]]=1;
for(int j=1; j<n; j++)
{
dp2.clear();
for(int k=0; k<=sum; k++)
{
if(dp1[k])
{
dp2[k]=1;
dp2[k+fa[j]]=1;
dp2[abs(k-fa[j])]=1;
}
}
for(int k=0; k<=sum; k++)
{
if(dp2[k]&&!dp1[k])
dp1[k]=1;
}
}
scanf("%d",&m);
for(int j=1; j<=m; j++)
{
scanf("%d",&exm);
if(dp1[exm])
printf("YES\n");
else
printf("NO\n");
}
}
}
return 0;
}
hdu 5616的更多相关文章
- HDU 5616 Jam's balance(Jam的天平)
HDU 5616 Jam's balance(Jam的天平) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- HDU 5616 Jam's balance(DP)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java ...
- hdu 5616 Jam's balance 正反背包+转换
http://acm.hdu.edu.cn/showproblem.php?pid=5616 思路 题目中蕴含着两种需要计算的重量 1. 从所有的砝码中挑出任意种2.(转换的思想)在天平的两端都挑出这 ...
- HDU 5616:Jam's balance(背包DP)
http://acm.hdu.edu.cn/showproblem.php?pid=5616 题意:有n个物品,每个重量为w[i],有一个天平,你可以把物品放在天平的左边或者右边,接下来m个询问,问是 ...
- HDU 5616 Jam's balance(01背包)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java ...
- hdu 5616 Jam's balance(dp 正反01背包)
来自官方题解: AC代码: #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream ...
- HDU 5616 Jam's balance
背包.dp[i]=1表示i这种差值能被组合出来,差值有负数,所以用sum表示0,0表示-sum,2*sum表示sum. 询问X的时候,只需看dp[sum+X]或者dp[sum-X]是否有一个为1,注意 ...
- HDU 5616 Jam's balance 背包DP
Jam's balance Problem Description Jim has a balance and N weights. (1≤N≤20)The balance can only tell ...
- Jam's balance HDU - 5616 (01背包基础题)
Jim has a balance and N weights. (1≤N≤20) The balance can only tell whether things on different side ...
随机推荐
- 定时任务 linux crontab 学习整理
1. 定时任务命令概念 crontab命令用于设置周期性被执行的指令.即设定脚本 按照规定时间执行相关的操作. 2.定时任务书写规范 * * * ...
- Python3 小工具-TCP发现
from scapy.all import * import optparse import threading import os def scan(ip): pkt=IP(dst=ip)/TCP( ...
- Alpha发布——Thunder团队
视频展示 视频链接: 爱奇艺: http://www.iqiyi.com/w_19ruzwru25.html (画质清晰,但可能需多次刷新或重新打开页面,此问题因电脑型号和网络而异) 优酷: ...
- 常用算法Java实现之冒泡排序
冒泡排序是所有排序算法中最基本.最简单的一种.思想就是交换排序,通过比较和交换相邻的数据来达到排序的目的. 具体流程如下: 1.对要排序的数组中的数据,依次比较相邻的两个数据的大小. 2.如果前面的数 ...
- c# 编译的dll看不见注释问题
1.项目属性---->生成----->勾选XML文档文件: 2.使用的时候该文件和dll放在一块.
- ajax 返回值问题
错误示例:function returnFlag(){ $.ajax({ type:"post", dataType:"json", data:JSON.str ...
- lintcode-30-插入区间
插入区间 给出一个 无重叠 的按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你要确保列表中的区间仍然有序且 不重叠 (如果有必要的话,可以合并区间). 样例 插入区间[2, 5] 到 [ ...
- LintCode-381.螺旋矩阵 II
螺旋矩阵 II 给你一个数n生成一个包含1-n^2的螺旋形矩阵 样例 n = 3 矩阵为 [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 标 ...
- Jenkins系列-Jenkins升级、迁移和备份
升级Jenkins Jenkins的开发迭代非常快,每周发布一个开发版本,长期支持版每半年更新一次(ps:大版本更新).如此频繁的更新,怎么升级呢? war:下载新版的war文件,替换旧版本war文件 ...
- netem设置了网卡的流量控制,为啥发包的延迟就搞不定呢?
为啥我用netem做了一个流量的控制 但是发送的时候,感觉真正发送数据的时候还是没有达到每一个数据包都是1s的延迟呀,这里的1s的延迟是啥意思啊? 这里的delay并不是说每个数据包都delay 5s ...